導讀 | 使用Nginx搭配PHP已有7年的經歷,這份經歷讓咱們學會如何爲高流量站點優化NGINX和PHP-fpm配置。 |
1. 將TCP切換爲UNIX域套接字
UNIX域套接字相比TCP套接字在loopback接口上能提供更好的性能(更少的數據拷貝和上下文切換)。javascript
但有一點須要牢記:僅運行在同一臺服務器上的程序能夠訪問UNIX域套接字(顯然沒有網絡支持)。php
upstream backend { # UNIX domain sockets server unix:/var/run/fastcgi.sock; # TCP sockets # server 127.0.0.1:8080; }
現代計算機硬件是多處理器的,NGINX能夠利用多物理或虛擬處理器。css
多數狀況下,你的Web服務器都不會配置爲處理多種任務(好比做爲Web服務器提供服務的同時也是一個打印服務器),你能夠配置NGINX使用全部可用的處理器,NGINX工做進程並非多線程的。java
運行如下命令能夠獲知你的機器有多少個處理器:node
Linux上 -linux
cat /proc/cpuinfo | grep processor
FreeBSD上 -nginx
sysctl dev .cpu | grep location
將nginx.conf文件中work_processes的值設置爲機器的處理器核數。web
同時,增大worker_connections(每一個處理器核心能夠處理多少個鏈接)的值,以及將"multi_accept"設置爲ON,若是你使用的是Linux,則也使用"epoll":json
# We have 16 cores worker_processes 16; # connections per worker events { worker_connections 4096; multi_accept on; }
以咱們的經驗來看,同一臺機器上多個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; }
這一點影響較大,由於高流量站點上的日誌文件涉及大量必須在全部線程之間同步的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;
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;
open_file_cache max=200000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;
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;
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;
# 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
持續監控打開鏈接的數目,空閒內存以及等待狀態線程的數目。
設置警報在超出閾值時通知你。你能夠本身構建這些警報,或者使用相似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/