无法理解为什么我的上游/CORS 配置失败。这阻碍了一些本地开发和测试。
我得到了一个请求的资源上不存在“Access-Control-Allow-Origin”标头当发出 API 请求时local.mysite.com:8081 to events.mysite.com.
这是我的服务器配置 /etc/nginx/sites-available/mysite
# the IP(s) on which your node server is running. I chose port 3000.
upstream mysite {
server 127.0.0.1:3000;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name mysite events.mysite.com;
access_log /var/log/nginx/mysite.log;
# pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
location / {
proxy_set_header Access-Control-Allow-Origin *;
# proxy_set_header 'Access-Control-Allow-Credentials' 'true'; # i've tried with and without this setting
proxy_set_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';
proxy_set_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_mysite/;
proxy_redirect off;
}
}
also,我尝试使用添加标题而不是 Access-Control-* 选项上的 proxy_set_header ,但那里也没有骰子。
我正在运行 Node.js 应用程序。我没有修改 Node 代码来处理 CORS...是我的 nginx 配置错误,还是可以,但我需要在 Node 中做其他事情?
CORS 标头必须提供给浏览器,而不是您的节点应用程序。所以你应该使用add_header
指令,或者更好的是,在您的应用程序中设置这些标头。这应该足够了。如果你使用withCredentials
取消注释适当的行。如果您使用使浏览器发送预检请求的东西,您应该正确处理“OPTIONS”请求。
location / {
add_header Access-Control-Allow-Origin *;
# add_header Access-Control-Allow-Credentials true;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_mysite/;
proxy_redirect off;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)