【问题标题】:CORS with reverse proxy Nginx server and nodejs won't work带有反向代理 Nginx 服务器和 nodejs 的 CORS 将不起作用
【发布时间】:2018-05-09 08:39:16
【问题描述】:

我正在使用 nodejs 的服务器上工作,该服务器在我租用的 VPS 上运行。从我电脑上的本地主机,我向服务器发送了一个请求,但它给我的只是:

请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:8086' 因此不允许访问。

但我在 de Nginx 配置的默认文件中有这个:

    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 标头,所以这里有什么问题。

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

【问题讨论】:

  • 您的节点服务器是否处理options 请求?如果不是,则您正在传递请求,然后被节点服务器拒绝

标签: node.js nginx http-headers cors


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-08-16
    • 2020-06-29
    • 2020-09-22
    • 2017-10-22
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2018-02-16
    • 2017-09-27
    相关资源
    最近更新 更多