Node.js nginx,上游,cors故障

Node.js nginx,上游,cors故障,node.js,nginx,cors,Node.js,Nginx,Cors,无法理解我的上游/CORS配置失败的原因。这会阻止一些本地开发和测试 当从local.mysite.com:8081向events.mysite.com发出API请求时,请求的资源上不存在“访问控制允许来源”标题 这是我的服务器配置,来自/etc/nginx/sites available/mysite # the IP(s) on which your node server is running. I chose port 3000. upstream mysite { server

无法理解我的上游/CORS配置失败的原因。这会阻止一些本地开发和测试

当从local.mysite.com:8081events.mysite.com发出API请求时,请求的资源上不存在“访问控制允许来源”标题

这是我的服务器配置,来自/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;
    }
 }
同样,我尝试在访问控制-*选项上使用添加标题而不是代理设置标题,但也没有骰子


我正在运行Node.js应用程序。我没有修改节点代码来处理CORS。。。是我的nginx配置错误,还是可以,但我需要在节点中执行其他操作?

必须将CORS头提供给浏览器,而不是节点应用程序。因此,您应该使用
add_header
指令,或者最好在应用程序中设置这些头。这应该足够了。如果您确实将
与凭据一起使用
请取消对相应行的注释。若你们使用某种东西使浏览器发送飞行前请求,你们应该正确处理“选项”请求

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;
}

没有骰子——我在上面提到我也尝试过添加_头。我已经在我的应用程序中尝试了超级简单的标题修改,但我对Node是新手,一定是做错了。基本上我有这样一个:
函数start(route,handle){function onRequest(request,response){response.header(“访问控制允许源”,“*”);response.header(“访问控制允许头”,“X-request-With”)…server.js的其余部分
看起来在本地主机上,我现在收到一个失败的选项调用。net::ERR_CONNECTION\u refused如何请求此url?终于收到了--感谢您的响应,您是对的,我的Node.js代码中需要进行的更改仍然存在问题。在设置标头的函数中,我更改了Allow标头对此:
response.setHeader(“访问控制允许标题”、“来源、X请求、内容类型、接受”);