NGINX 反向代理和 Access-Control-Allow-Origin 问题

dav*_*esp 5 reverse-proxy nginx nginx-location nginx-reverse-proxy nginx-status

我正在配置一个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";
    }
}
Run Code Online (Sandbox Code Playgroud)

实验一:

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.
Run Code Online (Sandbox Code Playgroud)

实验二:

如果我启用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.
Run Code Online (Sandbox Code Playgroud)

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

实验3:

另一方面,如果我直接在浏览器上访问:
server url: https://server.hollywood.com/api/auth/loginAccess-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: *
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
Run Code Online (Sandbox Code Playgroud)

在我得到之前:“请求的资源上不存在‘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
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢!

小智 10

可能是您的 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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


小智 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";
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

希望能帮助到你。