以下是有效的Nginx安全防护措施:
基础安全加固
server_tokens off;
limit_except GET POST { deny all; }
location ~* \.(git|env|htaccess) { deny all; }
HTTPS与加密配置
return 301 https://$host$request_uri;
ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384';
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
请求限制与防护
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m; limit_conn addr 100;
client_max_body_size 10m; client_body_buffer_size 1k;
安全头部增强
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
访问控制与监控
allow 192.168.1.0/24; deny all;
/var/log/nginx/access.log
和error.log
,结合工具(如GoAccess、ELK)高级防护(可选)
testssl.sh
或SSL Labs测试配置参考来源: