Ubuntu上Nginx性能优化实用技巧
一 基础配置优化
worker_processes auto; # 或设为 CPU 核心数
events {
worker_connections 4096;
multi_accept on;
}
二 内容传输与压缩
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_proxied any;
三 反向代理与缓存
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 / {
proxy_cache my_cache;
proxy_pass http://backend;
}
}
}
四 启用HTTP/2与TLS
server {
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# 其他SSL安全参数...
}
五 系统与运维实践
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_syn_backlog = 4096
net.core.somaxconn = 4096
sudo apt install goaccess
goaccess /var/log/nginx/access.log -f COMMON -o html > /var/www/html/goaccess.html