NGINX反向代理和访问控制允许原因问题

时间:2018-06-09 06:58:36

标签: nginx reverse-proxy nginx-location nginx-reverse-proxy nginx-status

我正在配置NGINX Reverse Proxy

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

然后上面的网页需要做出以下要求:
服务器网址: 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

我在浏览器控制台上遇到以下错误(在我的情况下是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.

实验2:

如果我启用上面的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.

我不知道为什么在该标题出现之前没有多个???

实验3:

另一方面,如果我直接在浏览器上转到:
服务器网址: https://server.hollywood.com/api/auth/login 注释掉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: *
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'标题。”但现在我在上面看到该字段位于响应标题上。

实验4:

如果我再次启用上面的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

您是否知道为什么我的前两个实验未能获得相对于Access-Control-Allow-Origin的错误?

谢谢!

2 个答案:

答案 0 :(得分:1)

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

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

要解决此问题,我使用了nginx proxy_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

答案 1 :(得分:0)

尝试此配置:

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的代码

希望有帮助。