以下正是這方面的一些提示和發(fā)起:
1. 將TCP切換為UNIX域套接字
UNIX域套接字對(duì)比TCP套接字在loopback接口上能提供更好的機(jī)能(更少的數(shù)據(jù)拷貝和上下文切換)。
但有一點(diǎn)需要緊記:僅運(yùn)行在同一臺(tái)處事器上的措施可以會(huì)見UNIX域套接字(顯然沒有網(wǎng)絡(luò)支持)。
upstream backend
{
# UNIX domain sockets
server unix:/var/run/fastcgi.sock;
# TCP sockets
# server 127.0.0.1:8080;
}
2. 調(diào)解事情歷程數(shù)
現(xiàn)代計(jì)較機(jī)硬件是多處理懲罰器的,NGINX可以操作多物理或虛擬處理懲罰器。
大都環(huán)境下,你的Web處事器都不會(huì)設(shè)置為處理懲罰多種任務(wù)(好比作為Web處事器提供處事的同時(shí)也是一個(gè)打印處事器),你可以設(shè)置NGINX利用所有可用的處理懲罰器,NGINX事情歷程并不是多線程的。
運(yùn)行以下呼吁可以獲知你的呆板有幾多個(gè)處理懲罰器:
Linux上
cat /proc/cpuinfo | grep processor
FreeBSD上
sysctl dev .cpu | grep location
將nginx.conf文件中work_processes的值配置為呆板的處理懲罰器核數(shù)。
同時(shí),,增大worker_connections(每個(gè)處理懲罰器焦點(diǎn)可以處理懲罰幾多個(gè)毗連)的值,以及將"multi_accept"配置為ON,假如你利用的是Linux,則也利用"epoll":
# We have 16 cores
worker_processes 16;
# connections per worker
events
{
worker_connections 4096;
multi_accept on;
}
3. 配置upstream負(fù)載平衡
以我們的履向來看,同一臺(tái)呆板上多個(gè)upstream后端對(duì)比單個(gè)upstream后端可以或許帶來更高的吞吐量。
譬喻,假如你想支持最大1000個(gè)PHP-fpm子歷程(children),可以將該數(shù)字平均分派到兩個(gè)upstream后端,各自處理懲罰500個(gè)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. 禁用會(huì)見日志文件
這一點(diǎn)影響較大,因?yàn)楦?a href="http://www.qzkangyuan.com/cnidc/cio/seo/2016/0911/3660.html">流量站點(diǎn)上的日志文件涉及大量必需在所有線程之間同步的IO操縱。
access_log off;
log_not_found off;
error_log /var/log/nginx-error.log warn;
若你不能封鎖會(huì)見日志文件,至少應(yīng)該利用緩沖:
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. 緩存被頻繁會(huì)見的文件相關(guān)的信息
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
7. 調(diào)解客戶端超時(shí)時(shí)間
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. 調(diào)解輸出緩沖區(qū)巨細(xì)
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調(diào)優(yōu)
# 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