2015-06-30 

(點擊上方藍字,可快速關注我們)


通常來說,一個優化良好的 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 iperf


vim /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 = 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


每次修訂配置之后都需要執行以下命令使之生效.


sysctl -p /etc/sysctl.conf


別忘了在配置修訂之后務必要進行網絡 benchmark 測試,這樣可以觀測到具體是哪個配置修訂的優化效果最明顯。通過這種有效測試方法可以為你節省大量時間。


原文出處:dak1n1.com

譯文出處:伯樂在線 - Alick

譯文鏈接:http://blog.jobbole.com/87531/