Skip to content
On this page

Nginx 性能优化

Nginx本身就是一个高性能的Web服务器,但通过合理的配置可以进一步提升其性能。

工作进程和连接配置

优化worker进程

# 自动设置为CPU核心数
worker_processes auto;

# 绑定worker进程到特定CPU核心
worker_cpu_affinity auto;

# 设置worker进程优先级(可选)
worker_priority 0;

# 设置worker进程最大连接数
worker_connections 1024;

事件处理优化

events {
    # 使用高效的事件模型
    use epoll;  # Linux系统
    
    # 启用多接受连接
    multi_accept on;
    
    # 启用accept互斥锁
    accept_mutex off;
}

缓存配置

静态文件缓存

# 启用sendfile
sendfile on;

# 启用TCP_NOPUSH
tcp_nopush on;

# 启用TCP_NODELAY
tcp_nodelay on;

# 设置keepalive连接超时
keepalive_timeout 65;

# 最大keepalive请求数
keepalive_requests 100;

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_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        
        # 跳过缓存的条件
        proxy_cache_bypass $http_cache_control;
        
        # 添加缓存状态头
        add_header X-Cache-Status $upstream_cache_status;
        
        proxy_pass http://backend;
    }
}

压缩优化

Gzip压缩配置

# 启用gzip
gzip on;

# 启用压缩响应长度阈值
gzip_min_length 1024;

# 压缩级别(1-9, 数值越大压缩比越高但CPU消耗越大)
gzip_comp_level 6;

# 压缩的HTTP版本
gzip_http_version 1.1;

# 启用压缩vary头
gzip_vary on;

# 压缩的文件类型
gzip_types
    text/plain
    text/css
    text/xml
    text/javascript
    application/javascript
    application/xml+rss
    application/json
    application/x-javascript
    application/xml
    image/svg+xml;

# 启用gzip_static
gzip_static on;

静态文件优化

静态文件缓存

location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
    access_log off;
}

location ~* \.(pdf|txt|html|htm)$ {
    expires 2d;
    add_header Cache-Control "public";
    access_log off;
}

静态文件服务优化

# 启用高效文件传输
sendfile on;

# 在一个数据包中发送头部和正文
tcp_nopush on;

# 立即发送数据包
tcp_nodelay on;

# 启用AIO
aio on;
aio_write on;

# 启用directio(大文件)
location /downloads {
    directio 4m;
    output_buffers 1 128k;
}

连接优化

连接池优化

# 设置客户端连接超时
client_header_timeout 60s;
client_body_timeout 60s;

# 设置代理连接超时
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;

# 设置缓冲区大小
client_body_buffer_size 128k;
client_header_buffer_size 1k;
client_max_body_size 10m;
large_client_header_buffers 4 4k;

# 设置代理缓冲区
proxy_buffer_size 4k;
proxy_buffers 8 4k;
proxy_busy_buffers_size 8k;

负载均衡优化

负载均衡配置

upstream backend {
    # 使用最少连接算法
    least_conn;
    
    # 启用健康检查
    server backend1.example.com max_fails=3 fail_timeout=30s;
    server backend2.example.com max_fails=3 fail_timeout=30s;
    server backend3.example.com max_fails=3 fail_timeout=30s;
    
    # 备用服务器
    server backup.example.com backup;
}

server {
    location / {
        proxy_pass http://backend;
        
        # 启用连接池
        keepalive 32;
    }
}

HTTP/2优化

server {
    listen 443 ssl http2;
    server_name example.com;
    
    # SSL配置
    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/private.key;
    
    # HTTP/2特定优化
    http2_max_field_size 16k;
    http2_max_header_size 32k;
    
    # 其他配置...
}

完整性能优化配置示例

# 全局优化
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 4096;
    use epoll;
    multi_accept on;
    accept_mutex off;
}

http {
    # 基本优化
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    keepalive_requests 1000;
    
    # 缓冲区优化
    client_header_timeout 60s;
    client_body_timeout 60s;
    client_max_body_size 10m;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 8k;
    
    # 压缩优化
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_comp_level 6;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/javascript
        application/xml+rss
        application/json
        application/x-javascript
        application/xml;
    
    # 缓存配置
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g 
                     inactive=60m use_temp_path=off;
    
    server {
        listen 80;
        server_name example.com;
        
        # 静态文件优化
        location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }
        
        # 动态内容缓存
        location / {
            proxy_cache my_cache;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            proxy_cache_bypass $http_cache_control;
            add_header X-Cache-Status $upstream_cache_status;
            
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

监控和调优

启用状态监控

location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
}

性能测试

使用ab或wrk等工具测试性能:

bash
# 使用Apache Bench测试
ab -n 10000 -c 100 http://your-server/

# 使用wrk测试
wrk -t12 -c400 -d30s http://your-server/

通过这些优化配置,可以显著提升Nginx的性能表现,满足高并发访问的需求。