以下正是這方面的一些提示和發起:
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