如何使用Nginx模块增强安全性?
2025-04-14
5
参考资料
限制HTTP方法
location / { limit_except GET POST { deny all; } }
禁用服务器信息
server_tokens off;
添加安全头
add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; add_header X-XSS-Protection "1; mode=block";
启用HTTPS重定向
server { listen 80; server_name example.com; return 301 https://$host$request_uri; }
限制客户端上传大小
client_max_body_size 1M;
启用Basic认证
location /admin { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd; }
防止目录遍历
location ~* \.(env|log|htaccess)$ { deny all; }
限制IP访问
location /api { allow 192.168.1.0/24; deny all; }
启用CSP策略
add_header Content-Security-Policy "default-src 'self'";
禁用不必要的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 405; }
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。