繁体   English   中英

nginx,上游,CORS失败

[英]nginx, upstream, cors fail

无法理解为什么我的上游/ CORS配置失败。 这阻止了一些本地开发人员和测试。

local.mysite.com:8081events.mysite.com发出API请求时,请求的资源上没有“ Access-Control-Allow-Origin”标头

这是我在/ 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;
    }
 }

另外 ,我尝试在Access-Control- *选项上使用add_header而不是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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM