前言:
在众多网站搭建方案中,Nginx与 WordPress 的组合因其高效、稳定而广受欢迎。为了进一步提升 WordPress 网站的安全性,我们可以利用 Nginx 的配置规则来实现。今天,我将分享一些自己常用的 Nginx 防护规则,帮助大家更好地保护 WordPress 网站。这些规则可以根据个人需求进行适当调整。
通过在 Nginx 配置文件中添加特定的规则,我们可以禁止外部访问 WordPress 网站的核心 PHP 文件,从而有效防止恶意攻击。以下是一些实用的 Nginx 规则示例:
# 汇站网
# https://www.huizhanii.com/?p=38824
server {
listen 80;
server_name website.com;
# Redirect non-www to www (website.com -> www.website.com)
return 301 http://www.$server_name$request_uri;
}
server {
listen 80;
server_name www.website.com;
access_log /var/www/website.com/logs/access.log main;
error_log /var/www/website.com/logs/error.log warn;
root /var/www/website.com/public/htdocs;
index index.html index.htm index.php;
# 日志不记录 robots.txt
location = /robots.txt {
log_not_found off;
access_log off;
}
# 如果没有 favicon 文件则退出并返回 204 (没有错误内容)
location ~* /favicon\.ico$ {
try_files $uri =204;
expires max;
log_not_found off;
access_log off;
}
# 以下格式文件日志不需要记录
location ~* \.(js|css|png|jpg|jpeg|bmp|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
# Send the all shebang in one fell swoop
tcp_nodelay off;
# Set the OS file cache
open_file_cache max=1000 inactive=120s;
open_file_cache_valid 45s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
}
# http://wiki.nginx.org/WordPress
# 设置静态地址必须要添加的配置
# 如果你后台添加了固定链接,则需要添加以下配置
location / {
try_files $uri $uri/ /index.php?$args;
}
# 禁止访问 htaccess 文件
location ~ /\. {
deny all;
}
# nocgi cgi 等可执行的,不允许
location ~* \.(pl|cgi|py|sh|lua)\$ {
return 444;
}
#禁止访问 wp-config.php install.php 文件
location = /wp-config.php {
deny all;
}
location = /wp-admin/install.php {
deny all;
}
# 禁止访问 /wp-content/ 目录的 php 格式文件 (包含子目录)
location ~* ^/wp-content/.*.(php|phps)$ {
deny all;
}
# 允许内部分 wp-includes 目录的 .php 文件
location ~* ^/wp-includes/.*\.(php|phps)$ {
internal;
}
# 禁止访问 /wp-content/ 目录的以下文件格式 (包含子目录)
location ~* ^/wp-content/.*.(txt|md|exe)$ {
deny all;
}
# 禁止 uploads、images 目录下面的所有 php、jsp 访问
location ~ ^/(uploads|images)/.*\.(php|php5|jsp)$ {
deny all;
#return 403;
}
# 禁止访问目录 /conf/*
location ^~ /conf/ {
deny all;
}
# 注意:上述/conf/后面的斜杠不能少,否则所有以 conf 开头的目录或文件都将禁止访问。
## 禁止访问任何目录下的.sql 文件,禁止浏览器访问
location ~.*\.sql {
deny all;
}
# 这样,任一目录的 sql 文件都不会被用户访问到了。
# 处理 .php 文件
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_connect_timeout 180s;
fastcgi_send_timeout 180s;
fastcgi_read_timeout 180s;
fastcgi_intercept_errors on;
fastcgi_max_temp_file_size 0;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
# 限制登陆和管理 IP 地址
location ~ ^/(wp-admin|wp-login\.php) {
allow 1.2.3.4;
deny all;
## 下面是 fastcgi 方式
index index.php index.html index.htm;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
## 下面是代理方式的设置
proxy_pass http://apachebackend;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# wordpress 重写规则
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
# Add trailing slash to */wp-admin requests
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# 403 页面配置
error_page 403 http://cdn-home.mimvp.com/404.html; # 指定 CDN 页面
error_page 403 404.html; # 指定当前项目根目录下的 404.html 文件
}
通过这些规则,我们可以有效地防止外部访问 WordPress 网站的核心 PHP 文件,降低被攻击的风险。当然,这些规则可以根据个人需求进行调整,以满足不同网站的安全防护需求。
总之,利用 Nginx 规则来加强 WordPress 网站的防护是一种简单而有效的方法。通过合理配置 Nginx 规则,我们可以在很大程度上提高 WordPress 网站的安全性,保障网站的正常运行。希望这些规则对大家有所帮助,也欢迎大家分享自己的防护经验。
本文章已结束,如转载请注明:汇站网 » WordPress 网站安全防护:利用 Nginx 规则屏蔽核心 PHP 文件