插件窝 干货文章 Nginx服务器的高并发处理和请求响应优化技巧

Nginx服务器的高并发处理和请求响应优化技巧

优化 Nginx Cache 连接 528    来源:    2025-04-13

Nginx服务器高并发处理与请求响应优化技巧

核心优化策略

1. 基础配置优化

worker_processes auto;  # 自动设置为CPU核心数
worker_rlimit_nofile 100000;  # 每个worker进程能打开的文件描述符数量

events {
    worker_connections 4096;  # 每个worker进程的最大连接数
    use epoll;  # Linux下使用epoll事件模型
    multi_accept on;  # 一个worker同时接受多个连接
}

2. 连接与缓冲优化

http {
    sendfile on;  # 启用sendfile系统调用
    tcp_nopush on;  # 仅在sendfile开启时有效,优化数据包发送
    tcp_nodelay on;  # 禁用Nagle算法,减少延迟

    keepalive_timeout 30;  # 保持连接超时时间
    keepalive_requests 1000;  # 单个连接最大请求数

    client_body_buffer_size 10K;  # 客户端请求体缓冲区大小
    client_header_buffer_size 1k;  # 客户端请求头缓冲区大小
    client_max_body_size 8m;  # 客户端请求体最大大小
    large_client_header_buffers 4 8k;  # 大请求头缓冲区
}

3. 静态资源优化

server {
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;  # 长期缓存静态资源
        access_log off;  # 关闭访问日志
        add_header Cache-Control "public";  # 公共缓存
    }

    # 启用Gzip压缩
    gzip on;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_vary on;
    gzip_comp_level 6;
}

高级优化技巧

1. 负载均衡配置

upstream backend {
    least_conn;  # 最少连接算法
    server backend1.example.com weight=5;
    server backend2.example.com;
    server backend3.example.com max_fails=3 fail_timeout=30s;

    keepalive 32;  # 保持到上游服务器的连接
}

server {
    location / {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}

2. 动态内容缓存

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m 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_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_lock on;  # 防止缓存击穿
        add_header X-Proxy-Cache $upstream_cache_status;
    }
}

3. 性能监控与日志优化

# 访问日志格式优化
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for" '
                'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

access_log /var/log/nginx/access.log main buffer=32k flush=5m;
error_log /var/log/nginx/error.log warn;

系统级优化

  1. 操作系统调优:

    • 增加文件描述符限制: ulimit -n 100000
    • 调整内核参数: net.core.somaxconn = 65535 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 30
  2. 硬件优化:

    • 使用SSD存储
    • 增加服务器内存
    • 考虑10Gbps网络接口
  3. 安全优化:

    limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
    
    server {
       location / {
           limit_req zone=req_limit burst=20 nodelay;
       }
    }
    

测试与验证

  1. 使用工具进行压力测试:

    • ab -n 100000 -c 1000 http://yourserver.com/
    • wrk -t12 -c400 -d30s http://yourserver.com/
  2. 监控关键指标:

    • 连接数: netstat -an | grep :80 | wc -l
    • 请求处理速率
    • 错误率
    • 响应时间分布

通过以上优化组合,Nginx可以轻松处理数万甚至数十万的并发连接,同时保持较低的响应时间。