明白了,我给你整理一个 针对 WordPress 的 Nginx 最强安全配置,涵盖:
- 禁止 WebShell 执行
- 防止上传目录 PHP 执行
- 防止 tmp/shm 目录被利用
- 防止爆破登录
- 禁止可疑请求和路径
- 兼顾性能和 SEO
我会用单站点配置为例,你可以根据需要调整到多站点。
1️⃣ Nginx server 块示例
假设你的 WordPress 安装在 /var/www/html,域名 example.com:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri; # 强制 HTTPS
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
root /var/www/html;
index index.php;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
# 防止敏感文件被访问
location ~* /(readme.html|license.txt|wp-config.php|\.env|\.git) {
deny all;
return 403;
}
# WordPress 核心保护
location ~* /wp-includes/.*\.php$ {
deny all;
}
location = /xmlrpc.php {
deny all;
return 403;
}
# 禁止 uploads 执行 PHP
location ~* /wp-content/uploads/.*\.php$ {
deny all;
}
# 禁止 plugins/themes 目录以外的 PHP 文件
location ~* /wp-content/(?!plugins|themes)/.*\.php$ {
deny all;
}
# tmp/shm 防止 PHP 执行
location ~* /tmp/.*\.php$ {
deny all;
}
location ~* /dev/shm/.*\.php$ {
deny all;
}
# WordPress URL 伪静态
location / {
try_files $uri $uri/ /index.php?$args;
}
# 阻止可疑请求
location ~* /(eval|base64_decode|shell_exec|system|passthru|proc_open|popen) {
return 403;
}
# 限制请求体大小(防止上传超大文件攻击)
client_max_body_size 20M;
# 日志
access_log /var/log/nginx/wp_access.log;
error_log /var/log/nginx/wp_error.log;
}
2️⃣ 说明和亮点
| 功能 | 配置点 | 作用 |
|---|---|---|
| 强制 HTTPS | return 301 https://$server_name$request_uri; | 所有流量加密 |
| 核心文件保护 | /wp-config.php /wp-includes/ | 防止直接访问 PHP 核心文件 |
| uploads 保护 | /wp-content/uploads/.*\.php$ | 阻止 WebShell 执行 |
| tmp/shm 防护 | /tmp/ /dev/shm/ | 防止恶意落地文件执行 |
| plugins/themes 检查 | `/wp-content/(?!plugins | themes)/` |
| 可疑函数拦截 | `eval | base64_decode |
| xmlrpc 禁止 | /xmlrpc.php | 防止爆破和 DDOS pingback |
| 请求体限制 | client_max_body_size 20M | 防止大文件攻击 |
| 缓存 | 静态文件缓存 | 提升性能 |
3️⃣ Linux + PHP 层加固(配合 Nginx 最佳)
- uploads 禁止执行 PHP
sudo chown -R www-data:www-data /var/www/html/wp-content/uploads
sudo chmod -R 755 /var/www/html/wp-content/uploads
- 禁止 tmp/shm 执行 PHP
编辑/etc/fstab:
tmpfs /tmp tmpfs defaults,noexec,nosuid 0 0
tmpfs /dev/shm tmpfs defaults,noexec,nosuid 0 0
sudo mount -o remount /tmp
sudo mount -o remount /dev/shm
- PHP 配置禁止危险函数
php.ini:
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,pcntl_exec
- 禁用 WP 后台编辑器
wp-config.php:
define('DISALLOW_FILE_EDIT', true);
define('DISALLOW_FILE_MODS', true);
4️⃣ WebShell 扫描 + 监控
- 安装 Wordfence / MalCare 做 WP 文件完整性检测
- 配合 Maldet + ClamAV 扫描服务器
/var/www/html - 定时 cron 脚本扫描上传目录 + tmp 目录
个人微信:ssevening

