Hero Image
Avoiding the Top 10 NGINX Configuration Mistakes - NGINX

Avoiding the Top 10 NGINX Configuration Mistakes - NGINX 1) 每個 worker 的檔案描述元(FD)不夠 問題點 worker_connections 只限制 單一 worker 可同時開啟的連線數(預設 512)。 但每個連線/檔案/暫存檔/日誌都會消耗 檔案描述元(FD),而 OS 預設每個 process 常見是 1024。 常見錯誤:只調大 worker_connections,卻沒有同步提高 FD 限制,導致 worker 提早耗盡 FD。 修正方式 在 main context 設定 worker_rlimit_nofile,至少為 worker_connections 的 2 倍(經驗值)。 同時確認系統總 FD 上限 fs.file-max 足夠: worker_rlimit_nofile * worker_processes 要明顯小於 fs.file-max。 # main context worker_connections 1024; # 在 events {} 內 worker_rlimit_nofile 2048; # 在 main context 補充 NGINX 當 proxy 時:client 連線 1 FD + upstream 連線 1 FD,可能還需要暫存檔 1 FD。 若被 DoS 打滿 FD,甚至可能無法登入機器處置,因此要預留系統餘裕。 2) error_log off 其實沒有關閉 error log 問題點 error_log 不支援 off 參數。 寫成 error_log off; 會讓 NGINX 產生一個名為 off 的檔案(通常在 /etc/nginx/)。 修正方式(不建議真的關閉) 若真的必須停寫 error log(例如磁碟極度有限),改導到 /dev/null 並限制等級: # main context error_log /dev/null emerg; 補充 這條生效前,NGINX 啟動/ reload 驗證設定的過程仍可能先寫到預設路徑(常見 /var/log/nginx/error.log)。 可用啟動參數 nginx -e <error_log_location> 指定啟動階段的 error log 位置。 3) 沒有對 upstream 啟用 keepalive(導致連線/來源埠耗盡) 問題點 預設:NGINX 對 upstream 每個 request 都新建連線,連線建立/關閉都有成本。 高流量時會放大 OS 資源消耗;且連線關閉後會進入 TIME-WAIT,可能導致 來源埠(ephemeral ports)耗盡,進而無法建立新連線。 修正方式 (A) 在每個 upstream {} 內加 keepalive

Hero Image
Go string format

Go string format The verb at the end defines the type and the interpretation of its corresponding argument. d - decimal integer o - octal integer O - octal integer with 0o prefix b - binary integer x - hexadecimal integer lowercase X - hexadecimal integer uppercase f - decimal floating point, lowercase F - decimal floating point, uppercase e - scientific notation (mantissa/exponent), lowercase E - scientific notation (mantissa/exponent), uppercase g - the shortest representation of %e or %f G - the shortest representation of %E or %F c - a character represented by the corresponding Unicode code point q - a quoted character U - Unicode escape sequence t - the word true or false s - a string v - default format #v - Go-syntax representation of the value T - a Go-syntax representation of the type of the value p - pointer address % - a double %% prints a single % Go string format indexing package main import ( "fmt" ) func main() { n1 := 2 n2 := 3 n3 := 4 res := fmt.Sprintf("There are %d oranges %d apples %d plums", n1, n2, n3) fmt.Println(res) // There are 2 oranges 3 apples 4 plums res2 := fmt.Sprintf("There are %[2]d oranges %d apples %[1]d plums", n1, n2, n3) fmt.Println(res2) // There are 3 oranges 4 apples 2 plums } Go string format precision package main import ( "fmt" ) func main() { fmt.Printf("%0.f\n", 16.540) // 17 fmt.Printf("%0.2f\n", 16.540) // 16.54 fmt.Printf("%0.3f\n", 16.540) // 16.540 fmt.Printf("%0.5f\n", 16.540) // 16.54000 } Go string format flags package main import ( "fmt" ) func main() { fmt.Printf("%+d\n", 1691) // +1691 fmt.Printf("%#x\n", 1691) // 0x69b fmt.Printf("%#X\n", 1691) // 0X69B fmt.Printf("%#b\n", 1691) // 0b11010011011 fmt.Printf("%10d\n", 1691) // 1691 fmt.Printf("%-10d\n", 1691) // 1691 fmt.Printf("%010d\n", 1691) // 0000001691 } Go string format width package main import ( "fmt" ) func main() { w := "falcon" n := 122 h := 455.67 fmt.Printf("%s\n", w) // falcon fmt.Printf("%10s\n", w) // falcon fmt.Printf("%d\n", n). // 122 fmt.Printf("%7d\n", n) // 122 fmt.Printf("%07d\n", n) // 0000122 fmt.Printf("%10f\n", h) // 455.670000 fmt.Printf("%11f\n", h) // 455.670000 fmt.Printf("%12f\n", h) // 455.670000 }

Hero Image
YAML 裡的字串很長該怎麼做?

YAML 裡的字串很長該怎麼做? 在 YAML 裡已經有規範此部份,在這種情況有四種方法可以幫助我們: |: 其下內容的換行,就是換行,最後一行會有換行。 >: 其下內容的換行,不會是換行,會變為一個很長的字串,最後會有換行。 |-: 其下內容的換行,就是換行,但最後一行不會有換行。 >-: 其下內容的換常,不會是換行,最後一行也不會有換行。 簡單的說,> 跟 >- 可以增加 YAML 的可讀性,又不會有多餘的換行符號。而 | 跟 |- 則可以讓字串跟定義的一致,在 YAML 裡看到換行,那字串裡就會有換行符號。 --- - name: Test long string hosts: all vars: s1: "hello" s2: | s2 this is my very very very long string line1 line2 line3 s3: > s3 this is my very very very long string line1 line2 line3 s4: |- s4 this is my very very very long string line1 line2 line3 s5: >- s5 this is my very very very long string line1 line2 line3 tasks: - name: s1 copy: content: "{{ s1 }}" dest: "/tmp/s1.txt" # hello% - name: s2 copy: content: "{{ s2 }}" dest: "/tmp/s2.txt" # s2 # this is my very very very # long string # line1 # line2 # line3 - name: s3 copy: content: "{{ s3 }}" dest: "/tmp/s3.txt" # s3 this is my very very very long string line1 line2 line3 - name: s4 copy: content: "{{ s4 }}" dest: "/tmp/s4.txt" # s4 # this is my very very very # long string # line1 # line2 # line3% - name: s5 copy: content: "{{ s5 }}" dest: "/tmp/s5.txt" # s5 this is my very very very long string line1 line2 line3%