【Nginx 进阶】Nginx 性能优化
在本章中,我们将详细介绍 Nginx 性能优化的各种方法和技巧,包括服务器硬件优化、Nginx 配置优化、缓存优化、连接管理等。

Nginx 以其高性能和高并发处理能力而闻名,但通过合理的配置和优化,可以进一步提升其性能。在本章中,我们将详细介绍 Nginx 性能优化的各种方法和技巧,包括服务器硬件优化、Nginx 配置优化、缓存优化、连接管理等。
1. 服务器硬件优化
1.1 硬件选择
选择合适的硬件是提升 Nginx 性能的基础。应考虑以下因素:
- CPU:多核 CPU 可以提高 Nginx 处理并发请求的能力,推荐选择高主频和多核的处理器。
- 内存:充足的内存可以避免频繁的内存交换,提高性能,特别是在处理大量静态文件和缓存时。
- 磁盘:SSD 比传统 HDD 拥有更快的读写速度,可以显著提高文件读取和写入性能。
1.2 操作系统优化
优化操作系统的配置也可以提升 Nginx 的性能:
文件描述符:增加文件描述符的最大值,确保 Nginx 能够处理大量并发连接:
ulimit -n 65536
在 /etc/security/limits.conf 中添加:
* soft nofile 65536
* hard nofile 65536
TCP 参数:调整 TCP 参数以提高网络性能,在 /etc/sysctl.conf 中添加:
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
应用更改:
sudo sysctl -p
2. Nginx 配置优化
2.1 工作进程数
将 Nginx 的工作进程数设置为等于或稍大于 CPU 核心数,以充分利用多核 CPU 的优势:
worker_processes auto;
2.2 工作进程连接数
增加每个工作进程的最大连接数,以支持更多的并发连接:
events {
worker_connections 1024;
}
2.3 启用 sendfile
sendfile 可以直接从文件系统读取文件并发送给客户端,减少内核和用户空间之间的拷贝,提升性能:
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
}
2.4 启用 keepalive 连接
keepalive 连接可以减少连接建立和关闭的开销,提高性能:
http {
keepalive_timeout 65;
keepalive_requests 100;
}
2.5 调整缓冲区大小
调整缓冲区大小以处理大请求和响应:
http {
client_body_buffer_size 16K;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
output_buffers 1 32k;
postpone_output 1460;
}
3. 缓存优化
3.1 配置缓存路径
配置缓存路径和缓存大小,以提高缓存性能:
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
3.2 启用缓存
在服务器块中启用缓存:
server {
listen 80;
server_name example.com;
location / {
proxy_cache my_cache;
proxy_pass http://backend;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3.3 配置 FastCGI 缓存
对于使用 FastCGI 的应用,可以配置 FastCGI 缓存:
fastcgi_cache_path /data/nginx/fastcgi_cache levels=1:2 keys_zone=fastcgi_cache:10m inactive=60m use_temp_path=off;
server {
listen 80;
server_name example.com;
location / {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_cache fastcgi_cache;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 404 1m;
}
}
4. 连接管理
4.1 启用 HTTP/2
HTTP/2 提供了更高效的网络传输,可以显著提高网站的性能:
server {
listen 443 ssl http2;
...
}
4.2 调整 worker_rlimit_nofile
增加 Nginx 进程可以打开的文件描述符数量:
worker_rlimit_nofile 65536;
4.3 调整缓冲区和时间限制
调整缓冲区和时间限制以处理大请求和响应:
client_body_timeout 12;
client_header_timeout 12;
send_timeout 10;
5. 压缩优化
启用 Gzip 压缩可以减少传输的数据量,提高页面加载速度:
http {
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 256;
gzip_comp_level 5;
}
6. 静态文件优化
6.1 配置缓存头
为静态文件设置缓存头可以减少重复请求,提高性能:
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
6.2 使用 open_file_cache
open_file_cache 可以缓存文件描述符,提高文件读取性能:
http {
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}
7. 性能监控
7.1 配置 stub_status 模块
stub_status 模块可以提供 Nginx 的运行状态,便于监控性能:
server {
listen 80;
server_name example.com;
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
}
7.2 使用外部监控工具
使用如 Prometheus、Grafana、Zabbix 等监控工具,可以实时监控 Nginx 的性能和健康状态。
8. 完整示例
下面是一个综合了多种性能优化配置的完整示例:
worker_processes auto;
worker_rlimit_nofile 65536;
events {
worker_connections 1024;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 100;
client_body_buffer_size 16K;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
output_buffers 1 32k;
postpone_output 1460;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 256;
gzip_comp_level 5;
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
server {
listen 80;
server_name example.com;
location / {
proxy_cache my_cache;
proxy_pass http://backend;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}
}
9. 总结
在本章中,我们详细介绍了 Nginx 性能优化的各种方法和技巧,包括服务器硬件优化、Nginx 配置优化、缓存优化、连接管理、压缩优化、静态文件优化和性能监控。通过这些优化措施,你可以显著提升 Nginx 的性能和稳定性。
