Nginx是Linux服务器最流行的Web服务器,正确配置能轻松支撑数万的并发访问。本文详解10个最影响性能的Nginx参数配置。

一、Worker进程配置

1. 自动设置worker数量

worker_processes auto;  # 自动等于CPU核心数

查看CPU核心数:lscpu | grep "CPU(s)"

2. 绑定worker到指定CPU

worker_cpu_affinity auto;  # 自动绑定到不同CPU核心

3. 设置worker最大连接数

worker_rlimit_nofile 65535;  # 单个worker最大打开文件数

二、事件处理模型

4. 选择高效的事件模块

events {
    use epoll;           # Linux高性能事件驱动模型
    worker_connections 65535;  # 单个worker最大并发连接数
    multi_accept on;     # 一次接受多个连接
}

三、HTTP连接优化

5. 保持连接(Keepalive)

http {
    # 客户端到Nginx的连接
    keepalive_timeout 65;
    keepalive_requests 10000;

    # Nginx到后端的连接(代理时)
    upstream backend {
        server 127.0.0.1:8080;
        keepalive 100;  # 保持100个空闲连接
    }
}

6. 启用Gzip压缩

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml;
gzip_buffers 16 8k;
gzip_window 16k;
gzip_history 1;

四、缓存配置

7. 浏览器缓存策略

http {
    # 静态资源长期缓存
    location ~* \.(css|js|jpg|jpeg|png|gif|ico|webp|svg|woff|woff2|ttf|eot)$ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
        access_log off;  # 不记录静态资源的访问日志
    }

    # HTML不缓存
    location ~* \.html$ {
        expires -1h;
        add_header Cache-Control "no-store, no-cache, must-revalidate";
    }
}

8. 代理缓存配置

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=api_cache:10m max_size=1g inactive=60m;

location /api/ {
    proxy_cache api_cache;
    proxy_cache_valid 200 60s;
    proxy_cache_key "$scheme$request_method$host$request_uri";
    add_header X-Cache-Status $upstream_cache_status;
    proxy_pass http://backend;
}

五、FastCGI/PHP优化

9. PHP-FPM进程池配置

编辑 /etc/php/8.1/fpm/pool.d/www.conf

pm = dynamic
pm.max_children = 50       ; 最大子进程数
pm.start_servers = 10      ; 启动时创建的子进程数
pm.min_spare_servers = 5   ; 最小空闲进程数
pm.max_spare_servers = 20  ; 最大空闲进程数
pm.max_requests = 5000     ; 进程处理多少请求后重启,防止内存泄漏

10. FastCGI缓存与超时配置

location ~ \.php$ {
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

    # 超时设置
    fastcgi_connect_timeout 60s;
    fastcgi_send_timeout 60s;
    fastcgi_read_timeout 60s;

    # 缓冲
    fastcgi_buffer_size 128k;
    fastcgi_buffers 256 16k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
}

六、SSL/TLS安全优化

启用HTTP/2

server {
    listen 443 ssl http2;
    server_name yourdomain.com;

    ssl_certificate /etc/ssl/certs/your-cert.pem;
    ssl_certificate_key /etc/ssl/private/your-key.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
    ssl_prefer_server_ciphers on;

    # 启用HSTS
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}

七、性能监控与调优

查看当前Nginx性能状态

# 查看worker进程状态
ps -ef | grep nginx

# 查看连接状态
netstat -an | grep ESTABLISHED | wc -l

# 查看nginx状态模块
curl http://127.0.0.1/nginx_status

Nginx状态模块

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

八、系统级优化

调整系统文件描述符限制

# /etc/security/limits.conf
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535

网络参数优化

# /etc/sysctl.conf 添加
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200

# 应用配置
sysctl -p

性能提升效果对比

配置项 默认值 优化后 提升效果
worker_processes 1 auto(CPU核数) 2-8倍
worker_connections 1024 65535 64倍
Gzip压缩 关闭 开启 带宽节省60-80%
浏览器缓存 30天 用户体验大幅提升

总结

Nginx优化的核心是:增加并发处理能力(worker、connections)、减少数据传输(Gzip、缓存)、优化请求处理流程(FastCGI、proxy)。每个参数需根据服务器硬件配置和业务特点调整,建议先在测试环境验证后再应用到生产环境。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。