Ngnix 配置允许多个域名跨域访问

当浏览器发起ajax请求到其他域名或者访问其他域名m3u8资源时,会出现跨域的问题,导致无法正确访问资源提示:

No'Access-Control-Allow-Origin' header is present on the requested resourse.Origin 'http//localhost:8088' is therefore not allowed access.

在nginx上的解决方案是配置Access-Control-Allow-Origin来解决,但是此参数只允许配置单个域名“add_header Access-Control-Allow-Origin baidu.com”或者“add_header Access-Control-Allow-Origin *;”允许全部域名,当我们需要允许多个域名跨域访问时却不好配置,可以用map来实现,具体代码如下:

 第一种方式:

map $http_origin $corsHost {
    default 0;
    "~http://www.haibakeji.com" http://www.haibakeji.com;
    "~http://m.haibakeji.com" http://m.haibakeji.com;
    "~http://wap.haibakeji.com" http://wap.haibakeji.com;
}

server
{
    listen 80;
    server_name www.haibakeji.com;
    root /nginx;
    location /
    {
        add_header Access-Control-Allow-Origin $corsHost;
    }
}

 原文网址:https://www.haibakeji.com/archives/249.html

第二种方式:

location ~* \.(ttf|ttc|otf|eot|woff|font.css)$ {

    if ($http_origin = 'http://backend.test.com') {

        add_header 'Access-Control-Allow-Origin' "$http_origin";

    }

    if ($http_origin = 'http://wap.test.com') {

        add_header 'Access-Control-Allow-Origin' "$http_origin";

    }

}

原文地址:https://www.jianshu.com/p/f2ec1d6af047

一个详细的说明网站:https://segmentfault.com/a/1190000003710973

转载于:https://www.cnblogs.com/liuxiaowei/p/9620994.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值