1、配置线程数和并发数
worker_processes 4; #cpu
events {
worker_connections 10240;#每一个进程打开的最大连接数,包含了nginx与客户端和nginx与upstream之间的连接
multi_accept on; #可以一次建立多个连接
use epoll; 使用epoll连接模型
}
2、配置后端Server的长连接
upstream server_pool{
server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
server localhsot:8081 weight=1 max_fails=2 fail_timeout=30s;
keepalive 300; #300个长连接
}
location /{
proxy_http_version 1.1
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upcrade";
proxy_pass http://server_pool/;
}
3、配置压缩
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE[1-6]\.(?!.*SV1)";
gzip_types text/plain ...
gzip_vary on;#Vary:Accept-Encoding
gzip_static on; #如果有压缩好的直接用
4、 操作系统优化
配置文件 /etc/sysctl.conf
sysctl -w net.ipv4.tcp_syncookies=1 #防止一个套接字在有过多试图连接到达时引起过载
sysctl -w net.core.somaxconn=1024 #默认128,连接队列
sysctl -w net.ipv4.tcp_fin_timeout=10 #timewait的超时时间
sysctl -w net.ipv4.tcp_tw_reuse=1 #os直接使用timewait的连接
sysctl -w net.ipv4.tcp_tw_recycle=0 #回收禁用
5、其他优化
sendfile on;#减少文件在应用和内核之间的拷贝
tcp_nopush on;#当数据包达到一定大小时再发送
tcp_nodelay off;#有数据随时发送