HTTP反向代理攻击是指攻击者利用配置不当的反向代理服务器,将恶意请求转发到内部服务器,从而绕过安全限制或获取敏感信息的攻击方式。
SSRF攻击(服务器端请求伪造)
请求走私攻击(Request Smuggling)
头部注入攻击
路径遍历攻击
location / {
proxy_pass http://backend;
# 只允许代理到特定的后端服务器
proxy_pass_request_headers on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# 禁止代理到内部地址
if ($http_x_forwarded_for ~* "^192\.168|^10\.|^172\.(1[6-9]|2[0-9]|3[0-1])") {
return 403;
}
}
location / {
proxy_pass http://backend;
# 清理可能被滥用的头部
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
# 禁止传递某些敏感头部
proxy_hide_header Server;
proxy_hide_header X-Powered-By;
}
http {
# 确保只使用HTTP/1.1
proxy_http_version 1.1;
# 禁用分块传输编码
proxy_request_buffering on;
# 设置严格的头部解析
underscores_in_headers off;
}
location / {
limit_except GET POST {
deny all;
}
proxy_pass http://backend;
}
location / {
# 只允许来自特定IP的访问
allow 192.168.1.0/24;
allow 10.0.0.1;
deny all;
proxy_pass http://backend;
}
http {
log_format security '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'proxy:$http_x_forwarded_for';
access_log /var/log/nginx/security.log security;
}
通过以上配置和措施,可以显著降低Nginx反向代理面临的HTTP攻击风险,保护内部网络资源的安全。