NGINX 反向代理和 Access-Control-Allow-Origin 问题 [英] NGINX Reverse Proxy and Access-Control-Allow-Origin issue

查看:178
本文介绍了NGINX 反向代理和 Access-Control-Allow-Origin 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在配置一个 NGINX 反向代理.

在浏览器上我去:
客户网址: https://www.hollywood.com

那么上面的网页需要做的请求是:
服务器网址: https://server.hollywood.com/api/授权/登录

Then the web page above needs to do requests to:
server url: https://server.hollywood.com/api/auth/login

这是对应的配置:server.hollywood.com:

server {
    listen 443 ssl;
    server_name             server.hollywood.com;
    # add_header 'Access-Control-Allow-Origin' "https://www.hollywood.com" always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
    add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
    ssl_certificate         ../ssl/lets-encrypt/hollywood.com.crt;
    ssl_certificate_key     ../ssl/lets-encrypt/hollywood.com.key;
    location /
    {
        proxy_pass http://localhost:9201;
        include "../proxy_params.conf";
    }
}

实验 1:

当我访问以下内容时,注释掉 Access-Control-Allow-Origin 行:
客户网址: https://www.hollywood.com

With the Access-Control-Allow-Origin line commented out, when I access to:
client url: https://www.hollywood.com

我在浏览器控制台(在我的例子中是 Chrome)上收到以下错误:

I get the following error on the browser console (Chrome in my case):

POST https://server.hollywood.com/api/auth/login/local 502 (Bad Gateway)
(index):1 Failed to load https://server.hollywood.com/api/auth/login/local: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.hollywood.com' is therefore not allowed access. The response had HTTP status code 502.

实验 2:

如果我启用上面的 Access-Control-Allow-Origin 行,那么我会进入浏览器终端:

If I enable the Access-Control-Allow-Origin line above, then I get on the browser terminal:

Failed to load https://server.hollywood.com/api/auth/login/local: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, https://www.hollywood.com', but only one is allowed. Origin 'https://www.hollywood.com' is therefore not allowed access.

我不知道为什么在那个标题不存在之前多次出现???

I don't know why multiple when before that header was not present???

实验 3:

另一方面,如果我直接在浏览器上访问:
服务器网址: https://server.hollywood.com/api/授权/登录注释掉 Access-Control-Allow-Origin 行后,我得到以下信息(在网络部分):

In the other hand, if I go directly on the browser to the:
server url: https://server.hollywood.com/api/auth/login with the Access-Control-Allow-Origin line commented out, I get the following (on the Network section):

Response Headers:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 139
Content-Security-Policy: default-src 'self'
Content-Type: text/html; charset=utf-8
Date: Sat, 09 Jun 2018 06:34:00 GMT
Server: nginx/1.13.12
X-Content-Type-Options: nosniff

在我得到之前:请求的资源上不存在‘Access-Control-Allow-Origin’标头."但现在我在上面看到该字段位于响应标题中.

Before I got: "No 'Access-Control-Allow-Origin' header is present on the requested resource." but now I see above that field is in there on the Response Headers.

实验 4:

如果我再次启用上面的 Access-Control-Allow-Origin 行,则会得到以下信息(在网络部分):

If I enable again the Access-Control-Allow-Origin line above, then I get the following (on the Network section):

Response Headers:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: https://www.hollywood.com
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 139
Content-Security-Policy: default-src 'self'
Content-Type: text/html; charset=utf-8
Date: Sat, 09 Jun 2018 06:34:58 GMT
Server: nginx/1.13.12
X-Content-Type-Options: nosniff

现在我得到了两倍的字段:Access-Control-Allow-Origin.

Now I get two times the field: Access-Control-Allow-Origin.

你知道为什么我的前 2 个实验没有得到与以下相关的错误:Access-Control-Allow-Origin?

Do you have any idea why my first 2 experiments are failing getting the errors relative to: Access-Control-Allow-Origin?

谢谢!

推荐答案

可能是您的 proxy_pass 背后的服务器也设置了 Access-Control-Allow-Origin 标头.

It could be that the server behind your proxy_pass was setting the Access-Control-Allow-Origin header as well.

对于未来有类似问题的读者来说,我发现我的 node.js 服务器由于某种原因传递了一个 Access-Control-Allow-Origin: '*' 标头,如以及我在 node.js 中设置的实际标头以限制 CORS.注释掉我的 node.js cors 中间件时,Access-Control-Allow-Origin: '*' 标头仍然存在.

For what it's worth for future readers with a similar problem, I found that my node.js server was passing an Access-Control-Allow-Origin: '*' header for some reason, as well as the actual header I'd set in node.js to restrict CORS. When commenting out my node.js cors middleware, the Access-Control-Allow-Origin: '*' header still remained.

为了解决这个问题,我使用了 nginx proxy_hide_header 指令删除来自 node.js 的标头并按原样手动添加它:

To resolve this, I used the nginx proxy_hide_header directive to remove the header coming from node.js and manually adding it as it should be:

# proxying the
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

# local node.js server
upstream websocket {
    server 127.0.0.1:3000;
}

server {
    server_name ...;
    # ...;

    # add the header here
    add_header Access-Control-Allow-Origin https://www.hollywood.com;

    # Websockets config
    location /socket.io/ {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        # do not pass the CORS header from the response of the proxied server to the
        # client
        proxy_hide_header 'Access-Control-Allow-Origin';

        proxy_pass http://websocket;
        proxy_http_version 1.1;
    }

    location / {
        # ...
        try_files $uri /index.html;
    }
}

谷歌搜索这个问题非常困难,因为大多数人都试图通过打开访问控制来修复 CORS!这是具有类似问题的另一个问题:

Googling this issue was pretty hard since most people are trying to fix CORS by making the Access-Control wide open! Here was another issue with similar problems:

https://serverfault.com/questions/751678/how-can-i-replace-access-control-allow-origin-header-in-proxy-response-with-ngin

这篇关于NGINX 反向代理和 Access-Control-Allow-Origin 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆