limit_req_zone $binary_remote_addr zone=example_com_login:10m rate=10r/m;
server {
listen 80;
listen [::]:80;
server_name example.com;
location ^~ /.well-known/acme-challenge/ {
root /var/www/_letsencrypt;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# Mozilla Intermediate(Mozilla Server Side TLS 6.0)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
# 不开 OCSP stapling:Let's Encrypt 已于 2025 年停止 OCSP 服务。
root /var/www/example.com;
index index.php index.html;
server_tokens off;
client_max_body_size 64m;
# 安全头(只写在 server 层;location 里一旦写 add_header 就要整组重复)
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
# CSP 需要按站点调整,确认不会挡掉自己的脚本后再启用:
# add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;
add_header Strict-Transport-Security "max-age=63072000" always;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 256;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;
# WordPress 加固(参考 nginxconfig.io)
# wp-includes 里的 PHP 不允许直接访问(TinyMCE 的 wp-tinymce.php 例外)
location ~* ^/wp-includes/(?!js/tinymce/wp-tinymce\.php$).+\.php$ {
deny all;
}
# 上传目录里的 HTML / Flash / phar 也不当页面执行
location ~* ^/wp-content/uploads/.+\.(?:s?html?|swf|phar)$ {
deny all;
}
# 配置文件、版本信息
location ~* ^/(?:wp-config\.php|wp-config-sample\.php|readme\.html|license\.txt)$ {
deny all;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# 禁止在上传/缓存目录执行 PHP
location ~* ^/wp-content/uploads/.*\.php$ {
deny all;
}
location = /xmlrpc.php {
deny all; # 不用 Jetpack/客户端发布时建议关闭
}
location = /wp-login.php {
# 每个 IP 每分钟 10 次,突发 5 次,超出返回 429
limit_req zone=example_com_login burst=5 nodelay;
limit_req_status 429;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_hide_header X-Powered-By;
}
# 隐藏文件(.git、.env 等)
location ~ /\.(?!well-known) {
deny all;
}
}