nginx 的异步 i/o 配置是提升高并发场景下性能的关键。通过合理配置异步机制,可以充分利用系统资源,减少阻塞操作,提高吞吐量。
以下是 nginx 异步 i/o 的核心配置方法和最佳实践:
一、异步 i/o 核心配置
1. 启用文件异步 i/o(linux 推荐)
http {
aio on; # 启用异步文件 i/o
aio_write on; # 启用异步写(仅对支持的文件系统有效)
# 大文件传输推荐配置
sendfile on; # 启用零拷贝传输
tcp_nopush on; # 合并小数据包,提高网络效率
}
2. 配置线程池处理阻塞操作
# 创建线程池(处理磁盘 i/o、ssl 等耗时操作)
thread_pool default threads=32 max_queue=65536;
http {
# 对文件操作使用线程池
aio threads=default;
# 对 ssl 操作使用线程池(高并发 https 场景)
ssl_engine device; # 指定 ssl 硬件加速设备(如 openssl)
ssl_threads 4; # ssl 处理线程数
}
二、事件驱动模型优化
1. 选择最优事件模块
events {
use epoll; # linux 推荐
# use kqueue; # freebsd/macos 推荐
# use iocp; # windows 推荐
worker_connections 65535; # 每个工作进程支持的最大连接数
multi_accept on; # 一次 accept 多个连接,提高吞吐量
}
2. 调整工作进程数
worker_processes auto; # 根据 cpu 核心数自动调整
三、异步代理配置(针对后端服务)
1. 代理请求的异步处理
http {
proxy_connect_timeout 5s; # 连接后端超时时间
proxy_send_timeout 60s; # 发送请求超时时间
proxy_read_timeout 60s; # 读取响应超时时间
proxy_buffer_size 16k; # 代理响应的第一个缓冲区大小
proxy_buffers 4 32k; # 代理响应的缓冲区数量和大小
proxy_busy_buffers_size 64k; # 繁忙时的缓冲区大小
# 对代理请求启用异步 i/o
proxy_request_buffering on; # 缓冲客户端请求(默认 on)
}
2. 负载均衡与异步健康检查
upstream backend {
least_conn; # 最少连接负载均衡算法
server backend1.example.com max_fails=3 fail_timeout=30s;
server backend2.example.com max_fails=3 fail_timeout=30s;
check interval=5000 rise=2 fall=3 timeout=1000 type=http; # 异步健康检查
check_http_send "get /health http/1.1\r\nhost: backend\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
四、长连接与异步响应
1. 启用 http 长连接
http {
keepalive_timeout 65; # 长连接超时时间
keepalive_requests 100; # 每个长连接允许的最大请求数
# 针对客户端的长连接
keepalive_disable msie6; # 禁用对 ie6 的长连接
# 针对后端服务器的长连接
proxy_http_version 1.1;
proxy_set_header connection ""; # 移除 connection 头,启用 http/1.1 长连接
}
2. 异步响应处理
location /async {
proxy_pass http://backend;
proxy_store on; # 启用响应存储(先写入磁盘再发送)
proxy_store_access user:rw group:rw all:r; # 存储文件权限
}
五、高级异步配置
1. 异步 fastcgi 配置(php 等)
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param script_filename $document_root$fastcgi_script_name;
# 异步 fastcgi 配置
fastcgi_buffer_size 16k;
fastcgi_buffers 4 32k;
fastcgi_busy_buffers_size 64k;
fastcgi_temp_file_write_size 64k;
# 禁用缓冲可能导致阻塞,需谨慎
# fastcgi_buffering off;
}
2. 异步 ssl 配置
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
# 异步 ssl 优化
ssl_session_cache shared:ssl:10m; # 会话缓存
ssl_session_timeout 10m; # 会话超时时间
ssl_prefer_server_ciphers on; # 优先使用服务器密码套件
# 启用 ocsp 装订(异步验证证书)
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
}
六、性能监控与调试
1. 启用状态页
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
2. 日志中记录异步指标
log_format async '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" pipe=$pipe time=$request_time';
access_log /var/log/nginx/access.log async;
七、注意事项
避免过度异步化:
- 并非所有操作都适合异步(如小文件读取),可能增加系统开销。
监控系统资源:
- 使用
top、htop监控 cpu / 内存,netstat监控连接数。
测试配置效果:
- 使用工具(如
wrk、ab)进行压力测试,对比不同配置的性能。
调整系统参数:
- 增加系统文件描述符限制:
# /etc/security/limits.conf nginx hard nofile 65535 nginx soft nofile 65535
总结
合理配置 nginx 的异步 i/o 可以显著提升高并发场景下的性能。关键是:
- 选择最优事件模型(epoll/kqueue)。
- 使用线程池处理耗时操作(磁盘 i/o、ssl)。
- 优化代理和负载均衡,减少阻塞点。
- 启用长连接,减少连接建立开销。
通过结合这些配置,nginx 可以在有限的资源下处理数万并发连接,保持低延迟和高吞吐量。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论