我正在配置一个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
吗?
谢谢!