您现在的位置是:首页 > 网站头条 > 乱七八糟 > 文章正文 乱七八糟

分享个自用的 Nginx 加强 WordPress 防护的规则

西安SEO优化 2023-03-13 11:34:22 乱七八糟 人已围观

简介Nginx+ WordPress 的组合是目前非常普及的组合了,我们完全可以借助 Nginx 规则来加强 WordPress 的防护,提高 WordPress 的安全性,今天明月就给大家分享个自用的 Nginx 针对 WordPress 的防护规则,部分规则大家只需要根据自己的需要进行调整即可。

  Nginx+ WordPress 的组合是目前非常普及的组合了,我们完全可以借助 Nginx 规则来加强 WordPress 的防护,提高 WordPress 的安全性,今天明月就给大家分享个自用的 Nginx 针对 WordPress 的防护规则,部分规则大家只需要根据自己的需要进行调整即可。
  1. #隐藏 nginx 版本. 
  2. server_tokens off; 
  3. #隐藏 PHP 版本 
  4. fastcgi_hide_header X-Powered-By; 
  5. proxy_hide_header X-Powered-By; 
  6. #禁止目录列表 
  7. autoindex off; 
  8. #验证浏览器行为防 CC 
  9. #if ($cookie_say != "hbnl$remote_addr"){ 
  10. #     rewrite .* "$scheme://$host$uri" redirect; 
  11.      #return 444; 
  12. #} 
  13. #屏蔽 IP 访问 
  14. deny 158.69.243.0/24;  # MJ12bot 
  15. deny 46.229.168.0/24;  # SemRush  
  16. deny 54.36.148.0/24;   # AhrefsBot  
  17. deny 54.36.149.0/24;   # AhrefsBot 
  18. #禁止 17ce.com 的测速 
  19. if ($http_referer ~* 17ce.com) { return 444; } 
  20. #禁止恶意 UA 以及为空的请求(^$) 
  21. if ($http_user_agent ~ "Go-http-client|Apache-HttpClient|lua-resty-http|loli_spider|ngx_lua"){ 
  22.     return 444
  23. # 禁止 Scrapy 等工具的抓取 
  24. if ($http_user_agent ~* (cdnunion_monitor|python-httpx|Wget|Scrapy|HttpClient|PostmanRuntime|ApacheBench|python-requests|Python-urllib|node-fetch)) { 
  25.         return 499
  26.     } 
  27. #禁止一些特殊的 UA 
  28. if ($http_user_agent ~ "Mozilla/4.0\ \(compatible;\ MSIE\ 6.0;\ Windows\ NT\ 5.1;\ SV1;\ .NET\ CLR\ 1.1.4322;\ .NET\ CLR\ 2.0.50727\)") {  
  29.    return 444
  30. # 屏蔽恶意后缀文件访问请求 
  31. if ($document_uri ~* \.(asp|aspx|jsp|swp|git|env|yaml|yml|sql|db|bak|ini|docx|doc|rar|tar|gz|zip|log|bak|conf)$) { return 444; } 
  32.  
  33. #禁止直接访问任何 php 文件 
  34. location ~* /(?:uploads|files|akismet)/.*.php$ { deny all; access_log off; log_not_found off;} 
  35. # 保护 WordPress 系统文件 
  36. location = /wp-admin/install.php { deny all; } 
  37. location = /nginx.conf { deny all; } 
  38. location ~ ^/user_extention/ { deny all; } 
  39. location ~ /\.htaccess$ { deny all; } 
  40. location ~ /readme\.html$ { deny all; } 
  41. location ~ /readme\.txt$ { deny all; } 
  42. location ~ ^/wp-config.php$ { deny all; } 
  43. location ~ ^/wp-admin/includes/ { deny all; } 
  44. location ~ ^/wp-includes/[^/]+\.php$ { deny all; } 
  45. location ~ ^.*/\.git/.*$ { deny all; } 
  46. location ~ ^.*/\.svn/.*$ { deny all; } 
  47. # 禁用 Uploads 目录的 PHP 
  48. location ~ ^/wp\-content/uploads/.*\.(?:php[1-7]?|pht|phtml?|phps)$ { deny all; } 
  49. # 禁用 Plugins 目录的 PHP 
  50. location ~ ^/wp\-content/plugins/.*\.(?:php[1-7]?|pht|phtml?|phps)$ { deny all; } 
  51. # 禁用 Themes 目录的 PHP,慎重使用会影响到部分主题的缩略图裁剪哦! 
  52. location ~ ^/wp\-content/themes/.*\.(?:php[1-7]?|pht|phtml?|phps)$ { deny all; } 
  53. # 不记录 favicon.ico 请求 
  54. location = /favicon.ico { log_not_found off; access_log off;} 
  55. # 不记录 robots.txt 请求 
  56. location = /robots.txt { log_not_found off; access_log off;} 
  57. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {expires 30d;} 
  58. location ~ .*\.(js|css)?$ { expires 30d;} 
  59. location ~ /\. {deny all;} 
  60. location ~ /\.ht { deny all; access_log off; log_not_found off;} 
  61. location ~ /\.user.ini { deny all; access_log off;log_not_found off;} 

 

  一般明月使用这些规则都是新建一个.conf 文件,在 Nginx 站点配置文件里单独 include 这个文件,重启 Nginx 生效,主要是为了根据不同站点需求进行细微的修改而已,理论上来说在宝塔面板里也可以这么用的,大家活学活用即可。


万晓博SEO推荐服务器:

https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=zx866zjo

文章评论

站点信息

  • 建站时间:2019年3月25日
  • 网站程序:PHPCMS V9.6 UTF-8
  • 建站流程分享新手建站流程
  • 博客模板:《今夕何夕》
  • 文章统计20 篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 网站地图网站地图
  • 微信公众号:扫描二维码,关注我们

打赏本站

  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!
  • 微信扫码:你说多少就多少~
  • 支付宝扫码:你说多少就多少~

客服在线

服务时间

周一至周日 9:00-21:00