CORS with reverse proxy Nginx server and nodejs won't work

主宰稳场 提交于 2019-12-13 08:05:19

问题


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:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8086' is therefore not allowed access.

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

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

As always thanks for helping!


回答1:


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



来源:https://stackoverflow.com/questions/47488382/cors-with-reverse-proxy-nginx-server-and-nodejs-wont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!