修的了电脑 敲得了代码
     写得了前端 稳得住后端

WordPress 禁止访问网站核心 PHP 文件,提高安全性,以nginx为例

WordPress 用的是 PHP 语言,禁止访客访问网站核心 PHP 文件能提高安全性。我们以 Nginx 的配置文件为例,来详细说明如何安全配置:禁用某些目录执行 PHP。

  1. server {
  2.  
  3. listen 80;
  4. server_name website.com;
  5. # Redirect non-www to www (website.com -> www.website.com)
  6. return 301 http://www.$server_name$request_uri;
  7. }
  8.  
  9. server {
  10.  
  11.  listen 80;
  12.  server_name www.website.com;
  13.  access_log /var/www/website.com/logs/access.log main;
  14.  error_log /var/www/website.com/logs/error.log warn;
  15.  root /var/www/website.com/public/htdocs;
  16.  index index.html index.htm index.php;
  17.  
  18. # 日志不记录 robots.txt
  19.  location = /robots.txt {
  20.  log_not_found off;
  21.  access_log off;
  22. }
  23.  
  24. # 如果没有 favicon 文件则退出并返回 204 (没有错误内容)
  25.  
  26.  location ~* /favicon\.ico$ {
  27.  try_files $uri =204;
  28.  expires max;
  29.  log_not_found off;
  30.  access_log off;
  31. }
  32.  
  33. # 以下格式文件日志不需要记录
  34.  
  35.  location ~* \.(js|css|png|jpg|jpeg|bmp|gif|ico)$ {
  36.  expires max;
  37.  log_not_found off;
  38.  access_log off;
  39. # Send the all shebang in one fell swoop
  40.  tcp_nodelay off;
  41. # Set the OS file cache
  42.  open_file_cache max=1000 inactive=120s;
  43.  open_file_cache_valid 45s;
  44.  open_file_cache_min_uses 2;
  45.  open_file_cache_errors off;
  46. }
  47.  
  48. # http://wiki.nginx.org/WordPress
  49. # 设置静态地址必须要添加的配置
  50. # 如果你后台添加了固定链接,则需要添加以下配置
  51.  
  52.  location / {
  53.  try_files $uri $uri/ /index.php?$args;
  54. }
  55.  
  56. # 禁止访问 htaccess 文件
  57.  
  58.  location ~ /\. {
  59.  deny all;
  60. }
  61.  
  62. # nocgi cgi等可执行的,不允许
  63.  
  64.  location ~* \.(pl|cgi|py|sh|lua)\$ {
  65. return 444;
  66. }
  67.  
  68. #禁止访问 wp-config.php install.php 文件
  69.  
  70.  location = /wp-config.php {
  71.  deny all;
  72. }
  73.  
  74.  location = /wp-admin/install.php {
  75.  deny all;
  76. }
  77.  
  78. # 禁止访问 /wp-content/ 目录的 php 格式文件 (包含子目录)
  79.  
  80.  location ~* ^/wp-content/.*.(php|phps)$ {
  81.  deny all;
  82. }
  83.  
  84. # 允许内部分 wp-includes 目录的 .php 文件 
  85.  
  86.  location ~* ^/wp-includes/.*\.(php|phps)$ {
  87. internal;
  88. }
  89.  
  90.  
  91. # 禁止访问 /wp-content/ 目录的以下文件格式 (包含子目录)
  92.  
  93.  location ~* ^/wp-content/.*.(txt|md|exe)$ {
  94.  deny all;
  95. }
  96.  
  97. # 禁止uploads、images目录下面的所有php、jsp访问
  98.  location ~ ^/(uploads|images)/.*\.(php|php5|jsp)$ {
  99.  deny all;
  100. #return 403;
  101. }
  102.  
  103. # 禁止访问目录 /conf/*
  104.  
  105.  location ^~ /conf/ {
  106.  deny all;
  107. }
  108.  
  109. # 注意:上述/conf/后面的斜杠不能少,否则所有以conf开头的目录或文件都将禁止访问。
  110.  
  111.  
  112.  
  113. ## 禁止访问任何目录下的.sql文件,禁止浏览器访问
  114.  
  115.  
  116.  location ~.*\.sql {
  117.  deny all;
  118. }
  119.  
  120. # 这样,任一目录的sql文件都不会被用户访问到了。 
  121.  
  122.  
  123. # 处理 .php 文件
  124.  
  125.  location ~ \.php$ {
  126.  try_files $uri =404;
  127.  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  128.  include /etc/nginx/fastcgi_params;
  129.  fastcgi_connect_timeout 180s;
  130.  fastcgi_send_timeout 180s;
  131.  fastcgi_read_timeout 180s;
  132.  fastcgi_intercept_errors on;
  133.  fastcgi_max_temp_file_size 0;
  134.  fastcgi_pass 127.0.0.1:9000;
  135.  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  136.  fastcgi_index index.php;
  137. }
  138.  
  139. # 限制登陆和管理IP地址
  140.  
  141.  location ~ ^/(wp-admin|wp-login\.php) {
  142.  allow 1.2.3.4;
  143.  deny all;
  144.  
  145. ## 下面是fastcgi 方式
  146.  index index.php index.html index.htm;
  147.  fastcgi_index index.php;
  148.  fastcgi_pass 127.0.0.1:9000;
  149.  include fastcgi.conf;
  150.  
  151. ## 下面是代理方式的设置
  152.  proxy_pass http://apachebackend;
  153.  proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
  154.  
  155.  proxy_set_header Host $host;
  156.  proxy_set_header X-Real-IP $remote_addr;
  157.  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  158. }
  159.  
  160. # wordpress 重写规则
  161.  
  162.  rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
  163.  rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
  164.  
  165. # Add trailing slash to */wp-admin requests
  166.  rewrite /wp-admin$ $scheme://$host$uri/ permanent;
  167.  
  168.  
  169. # 403页面配置
  170.  
  171.  error_page 403 http://cdn-home.mimvp.com/404.html; # 指定CDN页面
  172.  
  173.  error_page 403 404.html; # 指定当前项目根目录下的404.html文件 
  174. }
赞(1)
未经允许不得转载:流云溪|码农 » WordPress 禁止访问网站核心 PHP 文件,提高安全性,以nginx为例