Nginx参数优化
worker_processes 1;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
worker_priority 0;
worker_rlimit_nofile 65536;
events {worker_connections 65536; use epoll;
accept_mutex on;
multi_accept on;
}
keepalive_requests 500;
keepalive_timeout 65 60;
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_types text/plain application/Javascript application/x-Javascript text/css application/xml text/Javascript application/x-httpd-php image/gif image/png;
gzip_vary on;
open_file_cache max=10000 inactive=60s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors on;
系统参数优化
默认的Linux内核参数考虑的是最通用场景,不符合用于支持高并发访问的Web服务器的定义,根据业务特点来进行调整,当Nginx作为静态web内容服务器、反向代理或者提供压缩服务器的服务器时,内核参数的调整都是不同的,此处针对最通用的、使Nginx支持更多并发请求的TCP网络参数做简单的配置
优化内核参数
修改/etc/sysctl.conf
fs.file-max = 1000000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_rmem = 10240 87380 12582912
net.ipv4.tcp_wmem = 10240 87380 12582912
net.core.netdev_max_backlog = 8096
net.core.rmem_default = 6291456
net.core.wmem_default = 6291456
net.core.rmem_max = 12582912
net.core.wmem_max = 12582912
注意:以上的四个参数,需要根据业务逻辑和实际的硬件成本来综合考虑net.ipv4.tcp_synCOOKIEs = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_recycle = 1
net.core.somaxconn=262114
net.ipv4.tcp_max_orphans=262114
PAM 资源限制优化
在/etc/security/limits.conf 最后增加:
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535