gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:47:17 25 4
gpt4 key购买 nike

我正在配置一个 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";
}
}

实验一:

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.

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

实验三:

另一方面,如果我直接在浏览器上转到:
服务器网址: 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’ header 。”但现在我在上面看到该字段在响应标题中。

实验四:

如果我再次启用 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 ?

谢谢!

最佳答案

可能是您的 proxy_pass 背后的服务器正在设置 Access-Control-Allow-Origin标题也是如此。

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

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

# 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!这是另一个有类似问题的问题:

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

关于NGINX 反向代理和 Access-Control-Allow-Origin 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50771746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com