技术解析

遇到一个 Nginx 跨域问题,翻遍了资料还是解决不了
0
2021-05-19 20:15:06
idczone
       location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Si大带宽服务器nce,Cache-Control,Content-Type';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
} 

已经配置了这段代码,但是依旧提示跨域问题

Access to XMLHttpRequest at 'https://xxx.com/activity/faceCompare' from origin 'https://bb.xxx.com' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'https://bb.xxx.com, *', but only one is allowed.

求解决方案,谢谢


The 'Access-Control-Allow-Origin' header contains multiple values 'https://bb.xxx.com, *', but only one is allowed。这不是说只允许一个你配了俩?

所以 Nginx 配置没问题是,是前端地址配置多了么?

看情况是多配置了一个,我之前遇到过类似的,是因为有多层 nginx,然后有两层都配置了 add_header,就会出现这种情况,不知道你这个是不是?可以先把你自己设置的去掉,然后发个 options 请求看下响应头

你 location 外层是不是也配了 add_header?



bb.xxx.com
```nginx
server
{
listen 80;
listen 443 ssl http2;
server_name bb.xxx.com;
index index.php index.html index.htm default.php default.htm default.html;
root /home/xxx;

SSL 相关配置,请勿删除或修改下一行带注释的 404 规则
404/404.html;
/> if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
}
/>
错误页配置,可以注释、删除或修改
error_page 404 /404.html;
error_page 502 /502.html;
/>
/> location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}

SSL 证书验证目录相关设置
location ~ \.well-known{
allow all;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}

location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
access_log /www/wwwlogs/bb.log;
error_log /www/wwwlogs/bb.error.log;
}
```

xxx.com
```nginx
server
{
listen 443 ssl http2 default;
listen 80;
server_name xxx.com;
index index.html index.htm index.jsp;
root /home/face_fronted;

if ($scheme = http ) {return 301 https://$host$request_uri;}

/>
错误页配置,可以注释、删除或修改
error_page 404 /404.html;
error_page 502 /502.html;
/>

location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
}
location /face {
/> if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}

if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
/>
^/common/unauth https://xxx.com/face/common/unauth permanent;

proxy_pass http://127.0.0.1:9080/face;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
X-Forwarded-For $proxy_add_x_forwarded_for;
}



/> location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}

SSL 证书验证目录相关设置
location ~ \.well-known{
allow all;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}

location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
access_log /www/wwwlogs/xxx.com.log;
error_log /www/wwwlogs/xxx.error.log;
}
```

外层应该是没加

上游加了吧

没做 cdn 这样的,直接就是服务器访问呢的

可以看出你的 origin 里包含了*和域名,域名现在看不出来哪里加的。
你可以考虑先去掉 nginx 的这一条,如果能正常访问,你就要找这个域名是哪里加上去的。
btw,可能是前端开发环境影响?

proxy_hide_header Access-Control-Allow-Origin; 隐藏从后端返回 Access-Control-Allow-Origin 头

额.. 难道不是因为 allow-origin 配的"*" 的缘故吗? 你试试把 * 改成单域名试试?

这种情况是你配了两个一样的 header,我以前遇到过,我在 php 中输出了 header,结果测试环境的 nginx 也有人配置了跨域,就会报这个

都是直接后端进行这个配置。。

还有一种情况是,配置* 是不行的。。。有些必须要具体域名,不能*

被设置了 2 次。你们后端部分是不是也有设置一次。

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服