(點(diǎn)擊上方藍(lán)字,可快速關(guān)注我們)
通常來(lái)說(shuō),一個(gè)優(yōu)化良好的 Nginx Linux 服務(wù)器可以達(dá)到 500,000 – 600,000 次/秒 的請(qǐng)求處理性能,然而我的 Nginx 服務(wù)器可以穩(wěn)定地達(dá)到 904,000 次/秒 的處理性能,并且我以此高負(fù)載測(cè)試超過(guò) 12 小時(shí),服務(wù)器工作穩(wěn)定。
這里需要特別說(shuō)明的是,本文中所有列出來(lái)的配置都是在我的測(cè)試環(huán)境驗(yàn)證的,而你需要根據(jù)你服務(wù)器的情況進(jìn)行配置:
從 EPEL 源安裝 Nginx:
yum -y install nginx
備份配置文件,然后根據(jù)你的需要進(jìn)行配置:
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.)
# 這里的數(shù)值不能超過(guò) CPU 的總核數(shù),因?yàn)樵趩蝹€(gè)核上部署超過(guò) 1 個(gè) Nginx 服務(wù)進(jìn)程并不起到提高性能的作用。
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 最大可用文件描述符數(shù)量,同時(shí)需要配置操作系統(tǒng)的 "ulimit -n 200000",或者在 /etc/security/limits.conf 中配置。
worker_rlimit_nofile 200000;
# only log critical errors
# 只記錄 critical 級(jí)別的錯(cuò)誤日志
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)
# 配置單個(gè) Nginx 單個(gè)進(jìn)程可服務(wù)的客戶端數(shù)量,(最大值客戶端數(shù) = 單進(jìn)程連接數(shù) * 進(jìn)程數(shù) )
# 最大客戶端數(shù)同時(shí)也受操作系統(tǒng) socket 連接數(shù)的影響(最大 64K )
worker_connections 4000;
# essential for linux, optmized to serve many clients with each thread
# Linux 關(guān)鍵配置,允許單個(gè)線程處理多個(gè)客戶端請(qǐng)求。
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.
# 允許盡可能地處理更多的連接數(shù),如果 worker_connections 配置太低,會(huì)產(chǎn)生大量的無(wú)效連接請(qǐng)求。
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(文件描述符/文件句柄)
# 在我的設(shè)備環(huán)境中,通過(guò)修改以下配置,性能從 560k 請(qǐng)求/秒 提升到 904k 請(qǐng)求/秒。
# 我建議你對(duì)以下配置嘗試不同的組合,而不是直接使用這幾個(gè)數(shù)據(jù)。
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
# 將日志寫(xiě)入高速 IO 存儲(chǔ)設(shè)備,或者直接關(guān)閉日志。
# 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.
# 開(kāi)啟 sendfile 選項(xiàng),使用內(nèi)核的 FD 文件傳輸功能,這個(gè)比在用戶態(tài)用 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.
# 打開(kāi) tcp_nopush 選項(xiàng),Nginux 允許將 HTTP 應(yīng)答首部與數(shù)據(jù)內(nèi)容在同一個(gè)報(bào)文中發(fā)出。
# 這個(gè)選項(xiàng)使服務(wù)器在 sendfile 時(shí)可以提前準(zhǔn)備 HTTP 首部,能夠達(dá)到優(yōu)化吞吐的效果。
tcp_nopush on;
# don't buffer data-sends (disable Nagle algorithm). Good for sending frequent small bursts of data in real time.
# 不要緩存 data-sends (關(guān)閉 Nagle 算法),這個(gè)能夠提高高頻發(fā)送小數(shù)據(jù)報(bào)文的實(shí)時(shí)性。
tcp_nodelay on;
# Timeout for keep-alive connections. Server will close connections after this time.
# 配置連接 keep-alive 超時(shí)時(shí)間,服務(wù)器將在超時(shí)之后關(guān)閉相應(yīng)的連接。
keepalive_timeout 30;
# Number of requests a client can make over the keep-alive connection. This is set high for testing.
# 單個(gè)客戶端在 keep-alive 連接上可以發(fā)送的請(qǐng)求數(shù)量,在測(cè)試環(huán)境中,需要配置個(gè)比較大的值。
keepalive_requests 100000;
# allow the server to close the connection after a client stops responding. Frees up socket-associated memory.
# 允許服務(wù)器在客戶端停止發(fā)送應(yīng)答之后關(guān)閉連接,以便釋放連接相應(yīng)的 socket 內(nèi)存開(kāi)銷。
reset_timedout_connection on;
# send the client a "request timed out" if the body is not loaded by this time. Default 60.
# 配置客戶端數(shù)據(jù)請(qǐng)求超時(shí)時(shí)間,默認(rèn)是 60 秒。
client_body_timeout 10;
# If the client stops reading data, free up the stale client connection after this much time. Default 60.
# 客戶端數(shù)據(jù)讀超時(shí)配置,客戶端停止讀取數(shù)據(jù),超時(shí)時(shí)間后斷開(kāi)相應(yīng)連接,默認(rèn)是 60 秒。
send_timeout 2;
# Compression. Reduces the amount of data that needs to be transferred over the network
# 壓縮參數(shù)配置,減少在網(wǎng)絡(luò)上所傳輸?shù)臄?shù)據(jù)量。
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].";
啟動(dòng) Nginx 并配置起機(jī)自動(dòng)加載。
service nginx start
chkconfig nginx on
配置 Tsung 并啟動(dòng)測(cè)試,測(cè)試差不多 10 分鐘左右就能測(cè)試到服務(wù)器的峰值能力,具體的時(shí)間與你的 Tsung 配置相關(guān)。
[root@loadnode1 ~] vim ~/.tsung/tsung.xml
<server host="YOURWEBSERVER" port="80" type="tcp"/>
tsung start
你覺(jué)得測(cè)試結(jié)果已經(jīng)夠了的情況下,通過(guò) ctrl+c 退出,之后使用我們之前配置的別名命令 treport 查看測(cè)試報(bào)告。
WEB 服務(wù)器調(diào)優(yōu),第二部分:TCP 協(xié)議棧調(diào)優(yōu)
這個(gè)部分不只是對(duì) Ngiinx 適用,還可以在任何 WEB 服務(wù)器上使用。通過(guò)對(duì)內(nèi)核 TCP 配置的優(yōu)化可以提高服務(wù)器網(wǎng)絡(luò)帶寬。
以下配置在我的 10-Gbase-T 服務(wù)器上工作得非常完美,服務(wù)器從默認(rèn)配置下的 8Gbps 帶寬提升到 9.3Gbps。
當(dāng)然,你的服務(wù)器上的結(jié)論可能不盡相同。
下面的配置項(xiàng),我建議每次只修訂其中一項(xiàng),之后用網(wǎng)絡(luò)性能測(cè)試工具 netperf、iperf 或是用我類似的測(cè)試腳本 cluster-netbench.pl 對(duì)服務(wù)器進(jìn)行多次測(cè)試。
yum -y install netperf iperf
vim /etc/sysctl.conf
# Increase system IP port limits to allow for more connections
# 調(diào)高系統(tǒng)的 IP 以及端口數(shù)據(jù)限制,從可以接受更多的連接
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
# 設(shè)置協(xié)議棧可以緩存的報(bào)文數(shù)閥值,超過(guò)閥值的報(bào)文將被內(nèi)核丟棄
net.ipv4.tcp_max_syn_backlog = 3240000
# increase socket listen backlog
# 調(diào)高 socket 偵聽(tīng)數(shù)閥值
net.core.somaxconn = 3240000
net.ipv4.tcp_max_tw_buckets = 1440000
# Increase TCP buffer sizes
# 調(diào)大 TCP 存儲(chǔ)大小
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
每次修訂配置之后都需要執(zhí)行以下命令使之生效.
sysctl -p /etc/sysctl.conf
別忘了在配置修訂之后務(wù)必要進(jìn)行網(wǎng)絡(luò) benchmark 測(cè)試,這樣可以觀測(cè)到具體是哪個(gè)配置修訂的優(yōu)化效果最明顯。通過(guò)這種有效測(cè)試方法可以為你節(jié)省大量時(shí)間。
原文出處:dak1n1.com
譯文出處:伯樂(lè)在線 - Alick
譯文鏈接:http://blog.jobbole.com/87531/