Nginx 是目前最流行的 Web 服务器之一,它以高性能、低内存占用和强大的并发处理能力著称。对于 WordPress 网站来说,正确的 Nginx 配置不仅影响访问速度,还关系到安全性和稳定性。一份精心优化的 Nginx 配置可以显著提升网站的 Core Web Vitals 得分。

WordPress 的基础 Nginx 配置 包括 root 目录、index 文件和 PHP 处理。以下是一个标准的 WordPress Nginx 服务器配置:

server {
    listen 443 ssl http2;
    server_name yourdomain.com;
    root /var/www/wordpress;
    index index.php index.html;

    # SSL 证书配置
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    # 访问日志
    access_log /var/log/nginx/wordpress-access.log;
    error_log /var/log/nginx/wordpress-error.log;

    # 静态文件缓存设置
    location /wp-content/uploads/ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
    }

    location /wp-content/themes/ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
    }

    location /wp-content/plugins/ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
    }

    # 安全的 PHP 处理
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
        fastcgi_read_timeout 300;
        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
    }

    # WordPress 重写规则
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # 禁止访问敏感文件
    location ~ /(wp-config.php|.htaccess|.htpasswd|readme.html|license.txt) {
        deny all;
    }

    # 防止 PHP 文件被直接访问
    location ~* \.php$ {
        return 404;
    }
    location ~ ^/wp-content/uploads/.*\.php$ {
        deny all;
    }
}

Nginx 缓存策略 对性能影响巨大。除了静态文件的浏览器缓存,你还可以使用 Nginx 的 FastCGI Cache 实现页面级缓存:

# 在 http 块中定义缓存区域
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=wordpress:100m inactive=60m;

location ~ \.php$ {
    fastcgi_cache wordpress;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_valid 200 301 302 60m;
    fastcgi_cache_valid 404 1m;
    
    # 跳过缓存的条件
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    
    set $skip_cache 0;
    if ($http_cookie ~* "wordpress_logged_in") {
        set $skip_cache 1;
    }
    if ($request_method ~ ^(POST|PUT|DELETE)$) {
        set $skip_cache 1;
    }
    # ... PHP-FPM 配置
}

安全头(Security Headers) 是 Nginx 中必须配置的防护措施:

add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https:; style-src 'self' 'unsafe-inline' https:; img-src 'self' data: https:;" always;

Gzip 和 Brotli 压缩 可以减少传输体积:

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/rss+xml image/svg+xml;

brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/rss+xml image/svg+xml;

HTTPS 强制跳转 是 SEO 和安全的双重需求:

server {
    listen 80;
    server_name yourdomain.com;
    return 301 https://$server_name$request_uri;
}

限制请求速率(Rate Limiting) 可以防止暴力破解和 DDoS 攻击:

limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;

location /wp-login.php {
    limit_req zone=login burst=10 nodelay;
    # PHP 处理...
}

Nginx 与 WordPress 多站点的重写规则 有所不同,需要额外配置:

# 多站点子目录模式
location / {
    try_files $uri $uri/ /index.php?$args;
}
if (!-e $request_filename) {
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    rewrite ^/([_0-9a-zA-Z-]+/)?(wp-.*) /$2 last;
    rewrite ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 last;
}

# 多站点子域名模式
server_name *.yourdomain.com yourdomain.com;

Nginx 的配置需要根据服务器硬件和流量特征持续调优。建议在每次修改配置后使用 nginx -t 测试语法,然后 systemctl reload nginx 平滑加载新配置。定期查看 Nginx 的访问日志和错误日志,发现异常后及时调整。