zoukankan      html  css  js  c++  java
  • nginx 动态跨域配置

    方法一:

    server {
        .....
        set $cors_origin "";
        if ($http_origin ~* "a.xxxx.com") {
            set $cors_origin $http_origin;
        }
        if ($http_origin ~* "b.xxxx.com") {
            set $cors_origin $http_origin;
        }
        if ($http_origin ~* "c.xxxx.com") {
            set $cors_origin $http_origin;
        }
        add_header 'Access-Control-Allow-Origin' $cors_origin;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'X-Requested-With';
        .....
    }
    

    方法二:

    map $http_origin $cors_origin{
        default 0;
        "~http://a.xxxx.com" http://a.xxxx.com;
        "~http://b.xxxx.com" http://b.xxxx.com;
        "~http://b.xxxx.com" http://b.xxxx.com;
    }
     
    server
    {
        .....
        location / {
            add_header 'Access-Control-Allow-Origin' $cors_origin;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Headers' 'X-Requested-With';
        }
        .....
    }
    

    方法三:

    server
    {
        .....
        location / {
            if ( $http_origin ~ (.*).xxxxx.com ) {
                add_header Access-Control-Allow-Origin $http_origin;
                add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Headers' 'X-Requested-With';
            }
        .....
     }
    

    但上面的方法,在CDN场景下好像没作用。

    参考:https://www.cnblogs.com/sunmmi/articles/5956554.html

  • 相关阅读:
    P1168 中位数(对顶堆)
    P2341 [HAOI2006]受欢迎的牛
    P1967 货车运输
    树状数组的神操作QAQ
    P1063 能量项链
    P1429 平面最近点对(加强版)
    P2571 [SCOI2010]传送带
    4 Values whose Sum is 0
    UVA529 Addition Chains
    UVA307 Sticks
  • 原文地址:https://www.cnblogs.com/wshenjin/p/9071809.html
Copyright © 2011-2022 走看看