Nginx根據Origin配置禁止跨域訪問策略


產品需要通過某所的安全測評掃描,其中提出一個關於跨域策略配置不當的問題,如下:

 

 

 

這個需要根據客戶端傳遞的請求頭中的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;
        }

    }

}

 

最終效果:

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM