nginx安全设置
时间:2024-07-08 14:21:35|栏目:Nginx|点击:0 次
#防止文件被下载
location ~ \.(zip|rar|sql|bak|gz|7z)$ { return 444; }
屏蔽txt文件并排除多个.txt文件
location ~* \.txt$ { if ($request_uri !~ ^/(robots\.txt|sitemap\.txt)$) { return 404; } }
#屏蔽非常见蜘蛛(爬虫)
if ($http_user_agent ~* (SemrushBot|python|MJ12bot|AhrefsBot|AhrefsBot|hubspot|opensiteexplorer|leiki|webmeup)) { return 444; }
#禁止某个目录执行脚本
location ~* ^/(uploads|templets|data)/.*.(php|php5)$ { return 444; }
#禁止url中包含banned_word
if ($request_uri ~* "banned_word") { return 403; }
#禁止非GET|HEAD|POST方式的抓取
if ($request_method !~ ^(GET|HEAD|POST)$) { return 404; }
#禁止Scrapy等工具的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 404; }
#隐藏 nginx 版本.
server_tokens off;
#隐藏 PHP 版本
fastcgi_hide_header X-Powered-By; proxy_hide_header X-Powered-By;
#安全标头
add_header X-Content-Type-Options "nosniff" always; add_header X-XSS-Protection "1; mode=block" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header Referrer-Policy "origin" always;
#仅允许本站资源
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; frame-src 'self'; base-uri 'self'; form-action 'self'; object-src 'none';";
您可能感兴趣的文章
- 07-08nginx安全设置
- 02-01Nginx实现跨域使用字体文件的配置详解