我是靠谱客的博主 深情母鸡,这篇文章主要介绍nginx跨域问题解决方案,现在分享给大家,希望可以做个参考。

1.需要设置访问白名单

   map $host $app_data {
       default "-";
    }

    map $http_origin $corsHost {
        default 0;
        "~http://comment.test.com.cn" http://comment.test.com.cn;
        "~https://comment.test.com.cn" https://comment.test.com.cn;
        "~http://focus.test.com.cn" http://focus.test.com.cn;
        "~https://focus.test.com.cn" https://focus.test.com.cn;
    }
    add_header Access-Control-Allow-Origin $corsHost;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
配置在http模块 , "~https://focus.test.com.cn" https://focus.test.com.cn;此行为白名单

2.配置在location模块

add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,sw8';
		if($request_method = OPTIONS){
            return 200;
        }

以上两种方式任选一种即可

最后

以上就是深情母鸡最近收集整理的关于nginx跨域问题解决方案的全部内容,更多相关nginx跨域问题解决方案内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部