遇到一个 Nginx 跨域问题,翻遍了资料还是解决不了

2019-01-24 20:53:57 +08:00
 mifar
       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';
     }
} 

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

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.

求解决方案,谢谢

3961 次点击
所在节点    NGINX
15 条回复
Kyle18Tang
2019-01-24 21:11:48 +08:00
The 'Access-Control-Allow-Origin' header contains multiple values 'https://bb.xxx.com, *', but only one is allowed。这不是说只允许一个你配了俩?
mifar
2019-01-24 21:28:25 +08:00
@Kyle18Tang 所以 Nginx 配置没问题是,是前端地址配置多了么?
nzzzg
2019-01-24 21:37:37 +08:00
看情况是多配置了一个,我之前遇到过类似的,是因为有多层 nginx,然后有两层都配置了 add_header,就会出现这种情况,不知道你这个是不是?可以先把你自己设置的去掉,然后发个 options 请求看下响应头
Kyle18Tang
2019-01-24 21:48:27 +08:00
@mifar #2 你 location 外层是不是也配了 add_header?
mifar
2019-01-24 21:59:23 +08:00
@Kyle18Tang
@nzzzg

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-START SSL 相关配置,请勿删除或修改下一行带注释的 404 规则
#error_page 404/404.html;
#HTTP_TO_HTTPS_START
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';
}
}

#SSL-END

#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
error_page 404 /404.html;
error_page 502 /502.html;
#ERROR-PAGE-END


#禁止访问的文件或目录
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;}


#SSL-END

#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
error_page 404 /404.html;
error_page 502 /502.html;
#ERROR-PAGE-END

#REWRITE-END
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';
}
#处理跨域问题结束

#rewrite ^/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;
#proxy_set_header 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;
}
```
mifar
2019-01-24 21:59:47 +08:00
外层应该是没加
lululau
2019-01-24 22:04:10 +08:00
上游加了吧
mifar
2019-01-24 22:29:58 +08:00
@lululau 没做 cdn 这样的,直接就是服务器访问呢的
Vegetable
2019-01-24 22:36:24 +08:00
可以看出你的 origin 里包含了*和域名,域名现在看不出来哪里加的。
你可以考虑先去掉 nginx 的这一条,如果能正常访问,你就要找这个域名是哪里加上去的。
btw,可能是前端开发环境影响?
hasdream
2019-01-24 22:51:45 +08:00
proxy_hide_header Access-Control-Allow-Origin; # 隐藏从后端返回 Access-Control-Allow-Origin 头
kimchan
2019-01-25 09:13:07 +08:00
额.. 难道不是因为 allow-origin 配的"*" 的缘故吗? 你试试把 * 改成单域名试试?
dilu
2019-01-25 09:15:24 +08:00
这种情况是你配了两个一样的 header,我以前遇到过,我在 php 中输出了 header,结果测试环境的 nginx 也有人配置了跨域,就会报这个
yc8332
2019-01-25 09:58:19 +08:00
都是直接后端进行这个配置。。
yc8332
2019-01-25 09:58:58 +08:00
还有一种情况是,配置* 是不行的。。。有些必须要具体域名,不能*
br00k
2019-01-25 10:05:35 +08:00
被设置了 2 次。你们后端部分是不是也有设置一次。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/530379

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX