Nginx 以其高性能和高并发处理能力而有名,但通过合理的配置和优化,可以进一步提升其性能。在本章中,我们将详细先容 Nginx 性能优化的各种方法和技巧,包括做事器硬件优化、Nginx 配置优化、缓存优化、连接管理等。
1. 做事器硬件优化1.1 硬件选择
选择得当的硬件是提升 Nginx 性能的根本。应考虑以下成分:

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 = 65535net.ipv4.tcp_tw_reuse = 1net.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 的性能和稳定性。