Nginx 性能优化
2026/1/15大约 2 分钟
Nginx 性能优化
连接优化
worker 进程配置
# 工作进程数,通常设为 CPU 核心数
worker_processes auto;
# 每个 worker 的最大连接数
events {
worker_connections 10240;
use epoll; # Linux 使用 epoll
multi_accept on; # 一次接受多个连接
}keepalive 配置
http {
# 客户端 keepalive
keepalive_timeout 65;
keepalive_requests 1000;
# 上游 keepalive
upstream backend {
server 127.0.0.1:8080;
keepalive 32;
}
server {
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
}Gzip 压缩
http {
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 5;
gzip_types
text/plain
text/css
text/javascript
application/json
application/javascript
application/xml
application/xml+rss
image/svg+xml;
# 代理时也压缩
gzip_proxied any;
# 禁用 IE6 gzip
gzip_disable "msie6";
}缓存配置
静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
# 关闭访问日志
access_log off;
}
location ~* \.(html)$ {
expires 1h;
add_header Cache-Control "public, must-revalidate";
}代理缓存
http {
# 定义缓存路径
proxy_cache_path /var/cache/nginx
levels=1:2
keys_zone=my_cache:10m
max_size=10g
inactive=60m
use_temp_path=off;
server {
location /api/ {
proxy_pass http://backend;
# 启用缓存
proxy_cache my_cache;
proxy_cache_valid 200 10m;
proxy_cache_valid 404 1m;
proxy_cache_key $scheme$host$request_uri;
# 缓存状态头
add_header X-Cache-Status $upstream_cache_status;
# 缓存绕过
proxy_cache_bypass $http_cache_control;
}
}
}浏览器缓存策略
# 强缓存
location /static/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# 协商缓存
location /api/ {
add_header Cache-Control "no-cache";
etag on;
}
# 不缓存
location /user/ {
add_header Cache-Control "no-store, no-cache, must-revalidate";
expires -1;
}缓冲区优化
http {
# 客户端请求体缓冲
client_body_buffer_size 16k;
client_max_body_size 100m;
# 代理缓冲
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;
# 大文件传输
sendfile on;
tcp_nopush on;
tcp_nodelay on;
}超时配置
http {
# 客户端超时
client_header_timeout 15s;
client_body_timeout 15s;
send_timeout 15s;
# 代理超时
proxy_connect_timeout 10s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}限流配置
http {
# 请求速率限制
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
# 连接数限制
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
server {
location /api/ {
limit_req zone=req_limit burst=20 nodelay;
limit_conn conn_limit 10;
# 限流状态码
limit_req_status 429;
limit_conn_status 429;
}
}
}安全配置
server {
# 隐藏版本号
server_tokens off;
# 安全头
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# 禁止访问隐藏文件
location ~ /\. {
deny all;
}
# 禁止访问敏感文件
location ~* \.(git|env|sql|bak)$ {
deny all;
}
}完整优化配置
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 10240;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 日志
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time';
access_log /var/log/nginx/access.log main buffer=32k flush=5s;
# 基础优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 1000;
# 缓冲区
client_body_buffer_size 16k;
client_max_body_size 100m;
# Gzip
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 5;
gzip_types text/plain text/css application/json application/javascript;
# 限流
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
# 缓存
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m;
# 安全
server_tokens off;
include /etc/nginx/conf.d/*.conf;
}