meta-description: Nginx配置优化与调优完整教程,详解worker进程、缓存配置和Gzip压缩优化。

keywords: Nginx优化,Nginx配置,worker进程,缓存配置,Gzip压缩

# Nginx配置优化与调优完整教程

本文详细介绍Nginx的配置优化方法,包括worker进程调优、缓存配置和Gzip压缩优化。

## Nginx架构

Nginx采用事件驱动架构,性能远超Apache:

| 特性 | Nginx | Apache |

|------|-------|---------|

| 架构 | 事件驱动 | 进程/线程 |

| 内存占用 | 低 | 高 |

| 并发能力 | 高 | 中 |

| 静态文件 | 优秀 | 一般 |

## Worker进程优化

### 核心配置


# /etc/nginx/nginx.conf

# worker进程数(推荐:CPU核心数)
worker_processes auto;

# worker连接数(Linux默认1024,可调整)
events {
    worker_connections 65535;
    use epoll;  # Linux高性能事件模型
    multi_accept on;  # 一次接受所有新连接
}

### 优化系统限制


# 修改系统文件描述符限制
sudo nano /etc/security/limits.conf

# 添加:
* soft nofile 65535
* hard nofile 65535

# 立即生效
ulimit -n 65535

## 连接优化

### Keepalive连接


http {
    # 保持连接超时
    keepalive_timeout 65;
    
    # 保持连接请求数
    keepalive_requests 1000;
    
    # 关闭慢请求
    send_timeout 30;
}

### 客户端优化


http {
    # 客户端最大body大小
    client_max_body_size 64m;
    
    # 客户端body缓冲区
    client_body_buffer_size 128k;
    
    # 客户端header缓冲区
    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;
    
    # 读取超时
    client_body_timeout 30;
    client_header_timeout 30;
}

## 文件缓存优化

### Open File Cache


http {
    # 文件描述符缓存
    open_file_cache max=10000 inactive=30s;
    open_file_cache_min_uses 2;
    open_file_cache_valid 60s;
    open_file_cache_errors on;
}

### 静态文件缓存


# 图片缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
    add_header Cache-Control "public, immutable";
    access_log off;
}

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

## Gzip压缩优化

### 基础配置


http {
    # 启用Gzip
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    
    # 压缩级别(1-9,推荐5-6)
    gzip_comp_level 5;
    
    # 最小压缩大小
    gzip_min_length 1024;
    
    # 压缩类型
    gzip_types text/plain text/css text/xml text/javascript 
               application/javascript application/xml+rss 
               application/json;
}

### Brotli压缩(推荐)


# 需要Nginx编译Brotli模块
http {
    # 启用Brotli
    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/css application/javascript application/json;
}

## 缓冲区和超时

### 代理缓冲


http {
    # 代理缓冲
    proxy_buffering on;
    proxy_buffer_size 16k;
    proxy_buffers 8 16k;
    proxy_busy_buffers_size 32k;
    
    # 代理超时
    proxy_connect_timeout 30s;
    proxy_send_timeout 30s;
    proxy_read_timeout 30s;
}

### FastCGI缓冲(PHP)


http {
    # FastCGI缓冲
    fastcgi_buffering on;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 16 16k;
    
    # FastCGI超时
    fastcgi_connect_timeout 30s;
    fastcgi_send_timeout 30s;
    fastcgi_read_timeout 30s;
}

## 日志优化

### 访问日志缓冲


http {
    # 缓冲日志(减少磁盘I/O)
    access_log /var/log/nginx/access.log main buffer=32k flush=5s;
    
    # 静态文件不记录日志
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        access_log off;
    }
}

### 错误日志级别


    # 只记录错误
    error_log /var/log/nginx/error.log error;
}

## 安全优化

### 隐藏版本号


http {
    # 隐藏Nginx版本
    server_tokens off;
}

### 限制请求方法


# 只允许GET/POST/HEAD
if ($request_method !~ ^(GET|POST|HEAD)$) {
    return 444;
}

### 防止图片盗链


location ~* \.(jpg|jpeg|png|gif)$ {
    valid_referers none blocked *.example.com;
    if ($invalid_referer) {
        return 403;
    }
}

## SSL优化

### SSL会话缓存


http {
    # SSL会话缓存
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_session_tickets on;
}

### TLS优化


server {
    # 启用TLS 1.3
    ssl_protocols TLSv1.2 TLSv1.3;
    
    # 优化加密套件
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers off;
}

## 完整优化配置示例


# /etc/nginx/nginx.conf 优化版
user www-data;
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 65535;
    use epoll;
    multi_accept on;
}

http {
    # 基础优化
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    keepalive_requests 1000;
    
    # 文件缓存
    open_file_cache max=10000 inactive=30s;
    open_file_cache_valid 60s;
    open_file_cache_min_uses 2;
    
    # Gzip
    gzip on;
    gzip_comp_level 5;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/javascript;
    
    # 日志
    access_log off;
    error_log /var/log/nginx/error.log error;
    
    # 引入站点配置
    include /etc/nginx/sites-enabled/*;
}

## 性能测试


# 测试配置
sudo nginx -t

# 重载配置
sudo systemctl reload nginx

# 压力测试
ab -n 10000 -c 100 https://example.com/
wrk -t12 -c400 -d30s https://example.com/

## 监控指标

| 指标 | 命令 | 正常阈值 |

|------|------|----------|

| 活跃连接 | nginx -V | < worker_connections |

| 请求/秒 | access.log分析 | > 1000 |

| 平均响应时间 | wrk测试 | < 100ms |

| 错误率 | error.log | < 0.1% |

Nginx优化是提升网站性能的关键步骤,合理配置可提升50%以上性能。

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