nginx
主页 > 服务器 > nginx >

Nginx设置Access-Control-Allow-Origin多域名跨域实现

2024-11-20 | 佚名 | 点击:

修改配置文件

可通过如下配制进行多域名的设置。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

map $http_origin $corsHost {

   default 0;

   "~https://www.itbiancheng.com" https://www.itbiancheng.com;

   "~http://love.itbiancheng.com" http://love.itbiancheng.com;

}

server

{

   listen 80;

   server_name www.itbiancheng.com;

   root /nginx;

   location /

   {

      add_header Access-Control-Allow-Origin $corsHost;

   }

}

最近在做一个站,由于把样式和图片都独立出来了一个单独的域名,在移动端的时候访问提示跨域访问了,主要是因为css样式里面引用了字体文件,接下来吾爱编程为大家介绍一下,有需要的小伙伴可以参考一下:

1、错误提示

在这里插入图片描述

2、解决方法

nginx上的解决方案是配置Access-Control-Allow-Origin来解决,但是要么允许所有,要么允许单个的,都不能解决我的文件,经过一番查找找到了解决方法,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

map $http_origin $corsHost {

   default 0;

   "~https://www.itbiancheng.com" https://www.itbiancheng.com;

   "~http://love.itbiancheng.com" http://love.itbiancheng.com;

}

server

{

   listen 80;

   server_name www.itbiancheng.com;

   root /nginx;

   location /

   {

      add_header Access-Control-Allow-Origin $corsHost;

   }

}

原文链接:
相关文章
最新更新