導讀 | 使用Nginx搭配PHP已有7年的經曆,這份經曆讓我們學會如何為高流量站點優化NGINX和PHP-fpm配置。 |

以下正是這方面的一些提示和建議:
1. 将TCP切換為UNIX域套接字
UNIX域套接字相比TCP套接字在loopback接口上能提供更好的性能(更少的資料拷貝和上下文切換)。
但有一點需要牢記:僅運作在同一台伺服器上的程式可以通路UNIX域套接字(顯然沒有網絡支援)。
upstream backend
{
# UNIX domain sockets
server unix:/var/run/fastcgi.sock;
# TCP sockets
# server 127.0.0.1:8080;
}
2. 調整工作程序數
現代計算機硬體是多處理器的,NGINX可以利用多實體或虛拟處理器。
多數情況下,你的Web伺服器都不會配置為處理多種任務(比如作為Web伺服器提供服務的同時也是一個列印伺服器),你可以配置NGINX使用所有可用的處理器,NGINX工作程序并不是多線程的。
運作以下指令可以獲知你的機器有多少個處理器:
Linux上 -
cat /proc/cpuinfo | grep processor
FreeBSD上 -
sysctl dev .cpu | grep location
将nginx.conf檔案中work_processes的值設定為機器的處理器核數。
同時,增大worker_connections(每個處理器核心可以處理多少個連接配接)的值,以及将"multi_accept"設定為ON,如果你使用的是Linux,則也使用"epoll":
# We have 16 cores
worker_processes 16;
# connections per worker
events
{
worker_connections 4096;
multi_accept on;
}
3. 設定upstream負載均衡
以我們的經驗來看,同一台機器上多個upstream後端相比單個upstream後端能夠帶來更高的吞吐量。
例如,如果你想支援最大1000個PHP-fpm子程序(children),可以将該數字平均配置設定到兩個upstream後端,各自處理500個PHP-fpm子程序:
upstream backend {
server unix:/var/run/php5-fpm.sock1 weight=100 max_fails=5 fail_timeout=5;
server unix:/var/run/php5-fpm.sock2 weight=100 max_fails=5 fail_timeout=5;
}
4. 禁用通路日志檔案
這一點影響較大,因為高流量站點上的日志檔案涉及大量必須在所有線程之間同步的IO操作。
access_log off;
log_not_found off;
error_log /var/log/nginx-error.log warn;
若你不能關閉通路日志檔案,至少應該使用緩沖:
access_log /var/log/nginx/access.log main buffer=16k;
5. 啟用GZip
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
6. 緩存被頻繁通路的檔案相關的資訊
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
7. 調整用戶端逾時時間
client_max_body_size 500M;
client_body_buffer_size 1m;
client_body_timeout 15;
client_header_timeout 15;
keepalive_timeout 2 2;
send_timeout 15;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
8. 調整輸出緩沖區大小
fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_connect_timeout 3s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
reset_timedout_connection on;
server_names_hash_bucket_size 100;
9. /etc/sysctl.conf調優
# Recycle Zombie connections
net.inet.tcp.fast_finwait2_recycle=1
net.inet.tcp.maxtcptw=200000
# Increase number of files
kern.maxfiles=65535
kern.maxfilesperproc=16384
# Increase page share factor per process
vm.pmap.pv_entry_max=54272521
vm.pmap.shpgperproc=20000
# Increase number of connections
vfs.vmiodirenable=1
kern.ipc.somaxconn=3240000
net.inet.tcp.rfc1323=1
net.inet.tcp.delayed_ack=0
net.inet.tcp.restrict_rst=1
kern.ipc.maxsockbuf=2097152
kern.ipc.shmmax=268435456
# Host cache
net.inet.tcp.hostcache.hashsize=4096
net.inet.tcp.hostcache.cachelimit=131072
net.inet.tcp.hostcache.bucketlimit=120
# Increase number of ports
net.inet.ip.portrange.first=2000
net.inet.ip.portrange.last=100000
net.inet.ip.portrange.hifirst=2000
net.inet.ip.portrange.hilast=100000
kern.ipc.semvmx=131068
# Disable Ping-flood attacks
net.inet.tcp.msl=2000
net.inet.icmp.bmcastecho=1
net.inet.icmp.icmplim=1
net.inet.tcp.blackhole=2
net.inet.udp.blackhole=1
10. 監控
持續監控打開連接配接的數目,空閑記憶體以及等待狀态線程的數目。
設定警報在超出門檻值時通知你。你可以自己建構這些警報,或者使用類似ServerDensity的東西。
确認安裝了NGINX的stub_status子產品。該子產品預設并不會編譯進NGINX,是以可能你需要重新編譯NGINX -
./configure --with-http_ssl_module --with-http_stub_status_module --without-mail_pop3_module
--without-mail_imap_module --without-mail_smtp_module
make install BATCH=yes
免費提供最新Linux技術教程書籍,為開源技術愛好者努力做得更多更好:https://www.linuxprobe.com/