http {
map $http_origin $allow_cors {
default 1; # 默认不允许跨域
"~^(http://(dz.xxx.com)?)$" 1; # 允许特定域名跨域
"~http://localhost$" 1; # 允许localhost跨域
"~http://10.132.233.224$" 1; # 允许特定IP跨域,同时修正了正则表达式的错误
"~*" 0;
}
server {
listen 80;
location /testss/ {
if ($allow_cors != 1) {
return 403; # 如果不允许跨域,则返回403状态码
}
# 如果允许跨域,则设置相应的Access-Control-Allow-Origin头
if ($http_origin ~* (http://(dz.xxx.com|localhost|10.132.233.224))) {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
# 处理OPTIONS请求,某些浏览器在发送跨域请求前会发送OPTIONS请求进行预检
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
return 204; # 返回204状态码,表示不需要发送实体内容
}
# 其他location配置...
}
# 其他server配置...
}
}
在本次处理中,我们直接将手动注入请求Origin报头直接屏蔽,可修复相关注入漏洞