Nginx根据Origin配置禁止跨域访问策略

您所在的位置:网站首页 nginx跨域白名单 Nginx根据Origin配置禁止跨域访问策略

Nginx根据Origin配置禁止跨域访问策略

2024-01-20 02:06| 来源: 网络整理| 查看: 265

产品需要通过某所的安全测评扫描,其中提出一个关于跨域策略配置不当的问题,如下:

 

 

 

这个需要根据客户端传递的请求头中的Origin值,进行安全的跨站策略配置,目的是对非法的origin直接返回403错误页面,配置如下:

1、在http中定义一个通过map指令,定义跨域规则并返回是否合法

map $http_origin $allow_cros { "~^(https?://(dmp.xxxxxx.cn)?)$" 1; "~^\s" 1; "~*" 0; }

上述规则中,如果orgin的值为https://dmp.xxxxxx.cn或者空字符串,我们认为是合法的请求,返回数值1,如果是其它值,返回数值0

 

2、在server中根据$allow_cros的值进行请求拦截

if ($allow_cros = 0){ return 403; }

 

完整配置参考:

http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; map $http_origin $allow_cros { "~^(http?://(dmp.xxxxxx.cn)?)$" 1; "~^\s" 1; "~*" 0; } server { listen 80; server_name localhost; if ($allow_cros = 0){ return 403; } #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://192.168.10.105; } } }

 

最终效果:

 

 

 

 

 

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3