【发布时间】:2019-05-08 05:50:36
【问题描述】:
我有两个域
alpha.mydomain.com 和 api-alpha.mydomain.com
我正在尝试使用 nginx 作为代理
我收到了错误
在“https://api-alpha.mydomain.com/dup-check”访问 XMLHttpRequest 来自原点 'https://alpha.mydomain.com' 已被 CORS 阻止 策略:对预检请求的响应未通过访问控制 检查:没有“Access-Control-Allow-Origin”标头出现在 请求的资源。
我认为根据我的设置,请求不应该使用 api-alpha.mydomain.com 而是 127.0.0.1(并且没有收到 CORS 错误)
注意:: 我使用的是 cloudflare https,所以控制台错误是 https,cloudflare 是 SSL,并且与我的 nginx 服务器的 80 端口通信
这是我的 nginx 配置的一部分
server {
listen 80;
server_name alpha.mydomain.com ;
access_log /var/log/nginx.access_log main;
root /home/mydomain/react-front/dist;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name api-alpha.mydomain.com ;
access_log /var/log/nginx-api-alpha-access.log main;
location /{
proxy_pass http://127.0.0.1:4001/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}
这是来自 nginx-api-alpha-access.log 的条目
"OPTIONS /dup-check HTTP/1.1" 502 750 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" " -”
这是来自 /var/log/nginx/error.log 的条目
[error] 1280#1280: *12 connect() failed (111: Connection denied) while connection to upstream, client: 172.xx.xxx.xxx, server: api-mydomain.trigfig.com, request: "选项 /dup-check HTTP/1.1",上游:"http://127.0.0.1:4001/dup-check",主机:"api-alpha.mydomain.com"
谢谢,不知道我的配置中缺少什么
【问题讨论】: