通常來說,一個優化良好的 Nginx Linux 服務器可以達到 500,000 – 600,000 次/秒 的請求處理性能,然而我的 Nginx 服務器可以穩定地達到 904,000 次/秒 的處理性能,并且我以此高負載測試超過 12 小時,服務器工作穩定。
這里需要特別說明的是,本文中所有列出來的配置都是在我的測試環境驗證的,而你需要根據你服務器的情況進行配置:
從 EPEL 源安裝 Nginx:
yum -y install nginx
備份配置文件,然后根據你的需要進行配置:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig vim /etc/nginx/nginx.conf
# 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].";
啟動 Nginx 并配置起機自動加載。
service nginx start chkconfig nginx on
配置 Tsung 并啟動測試,測試差不多 10 分鐘左右就能測試到服務器的峰值能力,具體的時間與你的 Tsung 配置相關。
[root@loadnode1 ~] vim ~/.tsung/tsung.xml
<server host="YOURWEBSERVER" port="80" type="tcp"/>
tsung start
你覺得測試結果已經夠了的情況下,通過 ctrl+c 退出,之后使用我們之前配置的別名命令 treport 查看測試報告。
WEB 服務器調優,第二部分:TCP 協議棧調優
這個部分不只是對 Ngiinx 適用,還可以在任何 WEB 服務器上使用。通過對內核 TCP 配置的優化可以提高服務器網絡帶寬。
以下配置在我的 10-Gbase-T 服務器上工作得非常完美,服務器從默認配置下的 8Gbps 帶寬提升到 9.3Gbps。
當然,你的服務器上的結論可能不盡相同。
下面的配置項,我建議每次只修訂其中一項,之后用網絡性能測試工具 netperf、iperf 或是用我類似的測試腳本 cluster-netbench.pl 對服務器進行多次測試。
yum -y install netperf iperfvim /etc/sysctl.conf
# 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 = 3240000net.ipv4.tcp_max_tw_buckets = 1440000 # Increase TCP buffer sizes# 調大 TCP 存儲大小net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.ipv4.tcp_rmem = 4096 87380 16777216net.ipv4.tcp_wmem = 4096 65536 16777216net.ipv4.tcp_congestion_control = cubic
每次修訂配置之后都需要執行以下命令使之生效.
sysctl -p /etc/sysctl.conf
別忘了在配置修訂之后務必要進行網絡 benchmark 測試,這樣可以觀測到具體是哪個配置修訂的優化效果最明顯。通過這種有效測試方法可以為你節省大量時間。
常見優化配置項
一般來說nginx 配置文件中對優化比較有作用的為以下幾項:
1. worker_processes 8;
nginx 進程數,建議按照cpu 數目來指定,一般為它的倍數 (如,2個四核的cpu計為8)。
2. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
為每個進程分配cpu,上例中將8 個進程分配到8 個cpu,當然可以寫多個,或者將一
個進程分配到多個cpu。
3. worker_rlimit_nofile 65535;
這個指令是指當一個nginx 進程打開的最多文件描述符數目,理論值應該是最多打開文
件數(ulimit -n)與nginx 進程數相除,但是nginx 分配請求并不是那么均勻,所以最好與ulimit -n 的值保持一致。
現在在linux 2.6內核下開啟文件打開數為65535,worker_rlimit_nofile就相應應該填寫65535。
這是因為nginx調度時分配請求到進程并不是那么的均衡,所以假如填寫10240,總并發量達到3-4萬時就有進程可能超過10240了,這時會返回502錯誤。
查看linux系統文件描述符的方法:
[root@web001 ~]# sysctl -a | grep fs.file
fs.file-max = 789972fs.file-nr = 510 0 789972
4. use epoll;
使用epoll 的I/O 模型
(
補充說明:
與apache相類,nginx針對不同的操作系統,有不同的事件模型
A)標準事件模型
Select、poll屬于標準事件模型,如果當前系統不存在更有效的方法,nginx會選擇select或poll
B)高效事件模型
Kqueue:使用于 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X. 使用雙處理器的MacOS X系統使用kqueue可能會造成內核崩潰。
Epoll: 使用于Linux內核2.6版本及以后的系統。
/dev/poll:使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
Eventport:使用于 Solaris 10. 為了防止出現內核崩潰的問題, 有必要安裝安全補丁。
)
5. worker_connections 65535;
每個進程允許的最多連接數, 理論上每臺nginx 服務器的最大連接數為worker_processes*worker_connections。
6. keepalive_timeout 60;
keepalive 超時時間。
7. client_header_buffer_size 4k;
客戶端請求頭部的緩沖區大小,這個可以根據你的系統分頁大小來設置,一般一個請求頭的大小不會超過1k,不過由于一般系統分頁都要大于1k,所以這里設置為分頁大小。
分頁大小可以用命令getconf PAGESIZE 取得。
[root@web001 ~]# getconf PAGESIZE
4096
但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設置為“系統分頁大小”的整倍數。
8. open_file_cache max=65535 inactive=60s;
這個將為打開文件指定緩存,默認是沒有啟用的,max 指定緩存數量,建議和打開文件數一致,inactive 是指經過多長時間文件沒被請求后刪除緩存。
9. open_file_cache_valid 80s;
這個是指多長時間檢查一次緩存的有效信息。
10. open_file_cache_min_uses 1;
open_file_cache 指令中的inactive 參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的,如上例,如果有一個文件在inactive 時間內一次沒被使用,它將被移除。
關于內核參數的優化:
net.ipv4.tcp_max_tw_buckets = 6000
timewait 的數量,默認是180000。
net.ipv4.ip_local_port_range = 1024 65000
允許系統打開的端口范圍。
net.ipv4.tcp_tw_recycle = 1
啟用timewait 快速回收。
net.ipv4.tcp_tw_reuse = 1
開啟重用。允許將TIME-WAIT sockets 重新用于新的TCP 連接。
net.ipv4.tcp_syncookies = 1
開啟SYN Cookies,當出現SYN 等待隊列溢出時,啟用cookies 來處理。
net.core.somaxconn = 262144
web 應用中listen 函數的backlog 默認會給我們內核參數的net.core.somaxconn 限制到128,而nginx 定義的NGX_LISTEN_BACKLOG 默認為511,所以有必要調整這個值。
net.core.netdev_max_backlog = 262144
每個網絡接口接收數據包的速率比內核處理這些包的速率快時,允許送到隊列的數據包的最大數目。
net.ipv4.tcp_max_orphans = 262144
系統中最多有多少個TCP 套接字不被關聯到任何一個用戶文件句柄上。如果超過這個數字,孤兒連接將即刻被復位并打印出警告信息。這個限制僅僅是為了防止簡單的DoS 攻擊,不能過分依靠它或者人為地減小這個值,更應該增加這個值(如果增加了內存之后)。
net.ipv4.tcp_max_syn_backlog = 262144
記錄的那些尚未收到客戶端確認信息的連接請求的最大值。對于有128M 內存的系統而言,缺省值是1024,小內存的系統則是128。
net.ipv4.tcp_timestamps = 0
時間戳可以避免序列號的卷繞。一個1Gbps 的鏈路肯定會遇到以前用過的序列號。時間戳能夠讓內核接受這種“異常”的數據包。這里需要將其關掉。
net.ipv4.tcp_synack_retries = 1
為了打開對端的連接,內核需要發送一個SYN 并附帶一個回應前面一個SYN 的ACK。也就是所謂三次握手中的第二次握手。這個設置決定了內核放棄連接之前發送SYN+ACK 包的數量。
net.ipv4.tcp_syn_retries = 1
在內核放棄建立連接之前發送SYN 包的數量。
net.ipv4.tcp_fin_timeout = 1
如果套接字由本端要求關閉,這個參數決定了它保持在FIN-WAIT-2 狀態的時間。對端可以出錯并永遠不關閉連接,甚至意外當機。缺省值是60 秒。2.2 內核的通常值是180 秒,3你可以按這個設置,但要記住的是,即使你的機器是一個輕載的WEB 服務器,也有因為大量的死套接字而內存溢出的風險,FIN- WAIT-2 的危險性比FIN-WAIT-1 要小,因為它最多只能吃掉1.5K 內存,但是它們的生存期長些。
net.ipv4.tcp_keepalive_time = 30