通常來說,一個優化良好的 Nginx Linux 服務器可以達到 500,000 – 600,000 次/秒 的請求處理性能,然而我的 Nginx 服務器可以穩定地達到 904,000 次/秒 的處理性能,并且我以此高負載測試超過 12 小時,服務器工作穩定。
# This number should be, at maximum, the number of CPU cores on your system.
# (since nginx doesn't benefit from more than one worker per CPU.)
# 這里的數值不能超過 CPU 的總核數,因為在單個核上部署超過 1 個 Nginx 服務進程并不起到提高性能的作用。
worker_processes 24;
# Number of file descriptors used for Nginx. This is set in the OS with 'ulimit -n 200000'
# or using /etc/security/limits.conf
# Nginx 最大可用文件描述符數量,同時需要配置操作系統的 "ulimit -n 200000",或者在 /etc/security/limits.conf 中配置。
worker_rlimit_nofile 200000;
# only log critical errors
# 只記錄 critical 級別的錯誤日志
error_log /var/log/nginx/error.log crit
# Determines how many clients will be served by each worker process.
# (Max clients = worker_connections * worker_processes)
# "Max clients" is also limited by the number of socket connections available on the system (~64k)
# 配置單個 Nginx 單個進程可服務的客戶端數量,(最大值客戶端數 = 單進程連接數 * 進程數 )
# 最大客戶端數同時也受操作系統 socket 連接數的影響(最大 64K )
worker_connections 4000;
# essential for linux, optmized to serve many clients with each thread
# Linux 關鍵配置,允許單個線程處理多個客戶端請求。
use epoll;
# Accept as many connections as possible, after nginx gets notification about a new connection.
# May flood worker_connections, if that option is set too low.
# 允許盡可能地處理更多的連接數,如果 worker_connections 配置太低,會產生大量的無效連接請求。
multi_accept on;
# Caches information about open FDs, freqently accessed files.
# Changing this setting, in my environment, brought performance up from 560k req/sec, to 904k req/sec.
# I recommend using some varient of these options, though not the specific values listed below.
# 緩存高頻操作文件的FDs(文件描述符/文件句柄)
# 在我的設備環境中,通過修改以下配置,性能從 560k 請求/秒 提升到 904k 請求/秒。
# 我建議你對以下配置嘗試不同的組合,而不是直接使用這幾個數據。
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# Buffer log writes to speed up IO, or disable them altogether
# 將日志寫入高速 IO 存儲設備,或者直接關閉日志。
# access_log /var/log/nginx/access.log main buffer=16k;
access_log off;
# Sendfile copies data between one FD and other from within the kernel.
# More efficient than read() + write(), since the requires transferring data to and from the user space.
# 開啟 sendfile 選項,使用內核的 FD 文件傳輸功能,這個比在用戶態用 read() + write() 的方式更加高效。
sendfile on;
# Tcp_nopush causes nginx to attempt to send its HTTP response head in one packet,
# instead of using partial frames. This is useful for prepending headers before calling sendfile,
# or for throughput optimization.
# 打開 tcp_nopush 選項,Nginux 允許將 HTTP 應答首部與數據內容在同一個報文中發出。
# 這個選項使服務器在 sendfile 時可以提前準備 HTTP 首部,能夠達到優化吞吐的效果。
tcp_nopush on;
# don't buffer data-sends (disable Nagle algorithm). Good for sending frequent small bursts of data in real time.
# 不要緩存 data-sends (關閉 Nagle 算法),這個能夠提高高頻發送小數據報文的實時性。
tcp_nodelay on;
# Timeout for keep-alive connections. Server will close connections after this time.
# 配置連接 keep-alive 超時時間,服務器將在超時之后關閉相應的連接。
keepalive_timeout 30;
# Number of requests a client can make over the keep-alive connection. This is set high for testing.
# 單個客戶端在 keep-alive 連接上可以發送的請求數量,在測試環境中,需要配置個比較大的值。
keepalive_requests 100000;
# allow the server to close the connection after a client stops responding. Frees up socket-associated memory.
# 允許服務器在客戶端停止發送應答之后關閉連接,以便釋放連接相應的 socket 內存開銷。
reset_timedout_connection on;
# send the client a "request timed out" if the body is not loaded by this time. Default 60.
# 配置客戶端數據請求超時時間,默認是 60 秒。
client_body_timeout 10;
# If the client stops reading data, free up the stale client connection after this much time. Default 60.
# 客戶端數據讀超時配置,客戶端停止讀取數據,超時時間后斷開相應連接,默認是 60 秒。
send_timeout 2;
# Compression. Reduces the amount of data that needs to be transferred over the network
# 壓縮參數配置,減少在網絡上所傳輸的數據量。
gzip on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6].";
下面的配置項,我建議每次只修訂其中一項,之后用網絡性能測試工具 netperf、iperf 或是用我類似的測試腳本 cluster-netbench.pl 對服務器進行多次測試。
# Increase system IP port limits to allow for more connections
# 調高系統的 IP 以及端口數據限制,從可以接受更多的連接
net.ipv4.ip_local_port_range = 2000 65000
net.ipv4.tcp_window_scaling = 1
# number of packets to keep in backlog before the kernel starts dropping them
# 設置協議棧可以緩存的報文數閥值,超過閥值的報文將被內核丟棄
net.ipv4.tcp_max_syn_backlog = 3240000
# increase socket listen backlog
# 調高 socket 偵聽數閥值
net.core.somaxconn = 3240000
net.ipv4.tcp_max_tw_buckets = 1440000
# Increase TCP buffer sizes
# 調大 TCP 存儲大小
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_congestion_control = cubic
每次修訂配置之后都需要執行以下命令使之生效.