具有反向代理Nginx服务器和Node.js的CORS无法正常工作 [英] CORS with reverse proxy Nginx server and nodejs won't work

查看:99
本文介绍了具有反向代理Nginx服务器和Node.js的CORS无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nodejs在服务器上工作,该服务器正在我租用的VPS上运行.从我PC上的localhost,我向服务器发送了一个请求,但所有返回的信息是:

I am working on a server with nodejs the server is running on a VPS I hire. From the localhost on my pc, I send a request to the server but all it gives me back is:

所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问来源" http://localhost:8086 ".

但是我在de Nginx配置的默认文件中有此文件:

But I have this in my default file in de Nginx configuration:

    location / {
            proxy_set_header 'Access-Control-Allow-Origin' '*';
            proxy_set_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
            proxy_set_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';

            proxy_pass           http://localhost:8080;
            proxy_redirect       off;
            proxy_http_version   1.1;

            proxy_set_header     Upgrade     $http_upgrade;
            proxy_set_header     Connection  'upgrade';
            proxy_set_header     Host        $host;

            proxy_cache_bypass $http_upgrade;
    }

这是一个Access-Control-Allow-Origin标头,所以这里出了什么问题.

And here is an Access-Control-Allow-Origin header present so whats the problem here.

一如既往,感谢您的帮助!

As always thanks for helping!

推荐答案

这是我的nodejs代码中的一个错误,我不知道自己是否需要包含 response.setHeader('Access-Control-Allow-Origin', '*');

It was a mistake in my nodejs code I didnt know that I needed to include response.setHeader('Access-Control-Allow-Origin', '*');

这篇关于具有反向代理Nginx服务器和Node.js的CORS无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆