Nginx服务器性能调优
Nginx 配置文件
1、根据CPU内核数设置worker进程个数,以12核CPU为例,设置11个worker进程:
worker_processes 11;
worker_cpu_affinity 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000
001000000000 010000000000 100000000000;
2、事件模块
events {
worker_connections 65535; # 单个worker进程的最大连接数
use epoll;
}
3、系统限制性参数
worker_rlimit_nofile 65535; # 描述符最大限制
ulimit -a 命令可以查看当前系统的限制性参数;
在/etc/security/limits.conf 写入配置:
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
以上参数也可以写入 /etc/profile 文件使其生效。
4、其它
keepalive_timeout 60; # 客户端长连接超时时间
tcp_nopush on; #
user www-data; pid /var/run/nginx.pid; worker_processes auto; worker_rlimit_nofile 100000; events { worker_connections 2048; multi_accept on; use epoll; } http { server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on; access_log off; error_log /var/log/nginx/error.log crit; keepalive_timeout 10; client_header_timeout 10; client_body_timeout 10; reset_timedout_connection on; send_timeout 10; limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 100; include /etc/nginx/mime.types; default_type text/html; charset UTF-8; gzip on; gzip_disable "msie6"; gzip_proxied any; gzip_min_length 1000; gzip_comp_level 6; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
内核TCP选项
在/etc/sysctl.conf 写入配置:
net.ipv4.tcp_max_tw_buckets = 6000
设置timewait值
net.ipv4.ip_local_port_range = 1024 65000
设置允许系统打开的端口范围
net.ipv4.tcp_tw_recycle = 1
是否启用timewait快速回收
net.ipv4.tcp_tw_reuse = 1
允许将TIME-WAIT sockets 重新用于新的TCP连接
net.ipv4.tcp_syncookies = 1
SYN等待队列溢出时,使用Cookies来处理
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
记录尚未收到的客户端确认信息的连接请求的最大值
net.ipv4.tcp_timestamps = 0
使用时间戳作为序列号
net.ipv4.tcp_synack_retries = 1
设置SYN重试的次数
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_fin_timeout =1
net.ipv4.tcp_keepalive_time = 30
net.core.somaxconn = 262144
listen函数的backlog默认会将内核参数net.core.somaxconn限制到128
net.core.netdev_max_backlog = 262144
修改完成后执行 sysctl -p 命令使其生效。
参考文档:
http://ourjs.com/detail/5290c35850c832bd03000001