5

我正在配置一个NGINX Reverse Proxy.

在浏览器上,我转到:
客户端网址: https ://www.hollywood.com

那么上面的网页需要做请求到:
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";
    }
}

实验一:

Access-Control-Allow-Origin注释掉该行后,当我访问:
客户端网址: https ://www.hollywood.com

我在浏览器控制台(在我的情况下为 Chrome)上收到以下错误:

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.

实验二:

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

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.

我不知道为什么在那个标题不存在之前为什么会有多个???

实验三:

另一方面,如果我直接在浏览器上转到:
服务器 url: httpsAccess-Control-Allow-Origin ://server.hollywood.com/api/auth/login 并注释掉 该行,我会得到以下信息(在网络部分):

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'标头。” 但是现在我在上面看到该字段位于响应标头中。

实验四:

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

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.

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

谢谢!

4

2 回答 2

11

可能是您的 proxy_pass 后面的服务器也在设置Access-Control-Allow-Origin标头。

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

为了解决这个问题,我使用 nginxproxy_hide_header指令删除来自 node.js 的标头并手动添加它应该是:

# 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;
    }
}

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

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

于 2019-06-16T10:14:44.817 回答
1

试试这个配置:

server {
  listen 443 ssl;
  server_name server.hollywood.com;

  ssl_certificate ../ssl/lets-encrypt/hollywood.com.crt;
  ssl_certificate_key ../ssl/lets-encrypt/hollywood.com.key;

  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';

  location / {
    if ($request_method = 'OPTIONS') {
      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-Expose-Headers' 'Content-Length,Content-Range';
      proxy_pass http://localhost:9201;
      include "../proxy_params.conf";
    }
    if ($request_method = 'GET') {
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
      proxy_pass http://localhost:9201;
      include "../proxy_params.conf";
    }
  }
}

基于https://enable-cors.org/server_nginx.html的代码

希望能帮助到你。

于 2018-08-17T09:50:11.257 回答