本文详细介绍了Nginx服务器的高性能优化配置方案。重点从并发处理、传输效率、安全策略三个维度展开:1)通过CPU绑定、文件描述符优化和epoll事件模型提升并发能力;2)采用零拷贝技术、长连接复用和Gzip压缩优化传输效率;3)通过超时控制、请求限速和版本隐藏平衡安全与性能。同时提供了内核参数调优建议,并附完整配置示例,涵盖日志格式、连接管理、压缩策略等关键参数设置,帮助构建高并发、低延迟的Web服务环境。
目录
一、并发处理架构优化
1.进程与 CPU 绑定
- worker_processes auto;:自动匹配 CPU 核数,充分利用多核
- worker_cpu_affinity auto;:自动绑定 Worker 进程到 CPU 核心,减少上下文切换损耗(推荐)
2.文件描述符与连接数
- worker_rlimit_nofile 65535;:突破系统默认文件句柄限制,需同步修改系统级配置:
[worker@nginx-2 ~]$ cat /etc/security/limits.conf
...
* soft nofile 65535
* hard nofile 65535
- worker_connections 10240;:单进程最大并发连接数(需 ≤ worker_rlimit_nofile)
3.事件驱动模型
- use epoll:Linux 高并发首选(内核 2.6+)
- multi_accept on;:一次性接受所有新连接
4.汇总配置
[worker@nginx-2 conf]$ cat /usr/local/nginx/conf/nginx.conf
user www;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
events {
worker_connections 10240;
use epoll; # Linux高性能事件模型
multi_accept on; # 一次接受多个新连接
}
二、传输效率优化
优化传输效率,可以有效降低延迟与带宽
1.零拷贝技术
sendfile on; # 内核态直接传输文件,绕过用户态
tcp_nopush on; # 合并数据包,减少网络报文数量(需 sendfile 开启)
tcp_nodelay on; # 禁用 Nagle 算法,降低小包延迟:cite[3]:cite[4]:cite[6]
2.长连接复用
keepalive_timeout 65; # 客户端连接保持时间(秒)
keepalive_requests 10000; # 单连接最大请求数,避免频繁重建 TCP
三、静态文件压缩
# Gzip 压缩
gzip on; #启用 gzip 压缩功能
gzip_disable "MSIE [1-6]\."; #禁用对特定 User-Agent 的压缩
gzip_http_version 1.1; #只对 HTTP/1.1 及以上协议的请求进行压缩
gzip_comp_level 6; #压缩级别 1-9,数字越大压缩率越高但消耗更多 CPU
gzip_buffers 16 8k; #压缩的缓冲区 表示分配 16 个 8KB 的内存缓冲区用于压缩
gzip_min_length 1024; #只压缩大于 1KB (1024字节) 的文件
gzip_proxied any; # 对所有代理请求启用压缩
gzip_types text/plain text/css application/javascript application/xml application/json image/svg+xml application/x-font-ttf application/x-font-opentype application/vnd.ms-fontobject application/octet-stream;
四、安全与效率平衡策略
1.超时控制
client_header_timeout 15s; # 请求头超时
client_body_timeout 15s; # 请求体超时
send_timeout 10s; # 响应传输超时
reset_timedout_connection on; # 关闭超时连接释放资源
2.请求限制与防护
#限制接口/api/bigScreen/queryBuildingLocation 每分钟请求120次,可以处理120个突发请求
#zone=allips:10m:创建名为"allips"的共享内存区,大小为10MB,10MB大约可存储约16万个IP的状态信息
#rate=120r/m:限制速率为每分钟120个请求 (即每秒2个请求)
limit_req_zone $binary_remote_addr zone=allips:10m rate=120r/m;
server {
location ^~ /api/bigScreen/queryBuildingLocation {
#zone=allips:引用之前定义的allips共享内存区
#burst=120:设置突发容量为120个请求
#对突发请求不延迟处理,立即处理burst允许的数量,超过的直接返回503
limit_req zone=allips6 burst=120 nodelay;
proxy_pass http://houseFormPolice;
}
}
3.版本信息隐藏
server_tokens off; # 隐藏 Nginx 版本号
五、内核参数与系统级调优
# 调整 TCP 栈(/etc/sysctl.conf)
[worker@nginx-2 ~]$ vim /etc/sysctl.conf
...
net.core.somaxconn = 65535 # 最大连接队列
net.ipv4.tcp_max_syn_backlog = 65535 # SYN 队列长度
net.ipv4.tcp_tw_reuse = 1 # 快速复用 TIME_WAIT 端口
net.ipv4.ip_local_port_range = 1024 65535 # 扩大可用端口范围:cite[5]:cite[9]
[worker@nginx-2 ~]$ sysctl -p
六、完整示例版
[root@nginx ~]# cat nginx.conf
user www; # nginx进程启动用户
worker_processes auto; #与cpu核心一致即可
worker_cpu_affinity auto; # 自动绑定CPU核心
#中等流量Web应用 65536-262144 可应对数千并发
# worker_rlimit_nofile > worker_connections
# 需小于系统file-max值 cat /proc/sys/fs/file-max
worker_rlimit_nofile 65535; # 每个worker可打开文件数
error_log /var/log/nginx/error.log warn; # 错误日志
pid /run/nginx.pid;
events {
worker_connections 10240; # 限制每个进程能处理多少个连接,10240x[cpu核心]
use epoll; # 使用epoll高效网络模型 默认
multi_accept on; # 一次性接受所有新连接
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8; # 统一使用utf-8字符集
# 定义日志格式
log_format main '$http_x_real_ip | $remote_addr | $remote_user | [$time_iso8601] | "$request" | $status | $body_bytes_sent | $request_time | $upstream_addr | $upstream_status | $upstream_response_time | "$http_referer" | "$http_user_agent" | "$http_x_forwarded_for"';
#定义json日志格式
log_format json_access '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
access_log /var/log/nginx/access.log main; # 访问日志
server_tokens off; # 禁止浏览器显示nginx版本号
client_max_body_size 200m; # 文件上传大小限制调整
# 文件高效传输,静态资源服务器建议打开
sendfile on; #开启高效传输模式
tcp_nopush on; #开启tcp连接的非阻塞模式 ,配合 sendfile 提高网络效率
tcp_nodelay on; # 降低延迟
# 文件实时传输,动态资源服务建议打开,需要打开keepalive
keepalive_timeout 65; #设置http请求连接超时时间
keepalive_requests 10000; # 单连接最大请求数,避免频繁重建 TCP
send_timeout 10s; # 响应传输超时
reset_timedout_connection on; # 关闭超时连接释放资源
#Gzip 压缩
gzip on;
gzip_disable "MSIE [1-6]\."; #针对IE浏览器不进行压缩
gzip_http_version 1.1;
gzip_comp_level 6; #压缩级别
gzip_buffers 16 8k; #压缩的缓冲区
gzip_min_length 1024; #文件大于1kb才进行压缩
gzip_proxied any; # 对所有代理请求启用压缩
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg;
include /etc/nginx/conf.d/*.conf;
}