Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CORS不';尽管设置了标题,但仍无法工作_Javascript_Node.js_Nginx_Cors - Fatal编程技术网

Javascript CORS不';尽管设置了标题,但仍无法工作

Javascript CORS不';尽管设置了标题,但仍无法工作,javascript,node.js,nginx,cors,Javascript,Node.js,Nginx,Cors,我有一个应用程序,客户端通过带有Nginx的https从example.com向api.example.com发出多部分请求,然后api将文件上传到amazons3 它在我的机器上工作,但当其他人在不同的网络上尝试它时会中断。告诉我这个错误: [Error] Origin https://example.com is not allowed by Access-Control-Allow-Origin. [Error] Failed to load resource: Origin https:

我有一个应用程序,客户端通过带有Nginx的https从example.com向api.example.com发出多部分请求,然后api将文件上传到amazons3

它在我的机器上工作,但当其他人在不同的网络上尝试它时会中断。告诉我这个错误:

[Error] Origin https://example.com is not allowed by Access-Control-Allow-Origin.
[Error] Failed to load resource: Origin https://example.com is not allowed by Access-Control-Allow-Origin. (graphql, line 0)
[Error] Fetch API cannot load https://api.example.com/graphql. Origin https://example.com is not allowed by Access-Control-Allow-Origin.
我在API上使用cors npm包,如下所示:

app.use(cors());
所有这些都是通过DigitalOcean上的Nginx反向代理完成的。这是我的Nginx配置:

单个服务器在
/etc/nginx/conf.d/example.com.conf
/etc/nginx/conf.d/api.example.com.conf
上进行配置,几乎相同,只是地址和名称不同:

 server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name example.com;

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

        include snippets/ssl-params.conf;

        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://localhost:3000/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
        }
    }

当我在我的计算机上的本地主机上使用它时,它工作得非常好,但一旦我把它放到DigitalOcean上,我就无法上传。当我上传一个文件时,它只在这个多部分请求上中断,其他常规cors GET和POST请求工作。

问题可能不在nginx上,因为它只是一个移动问题。尝试使用,而不是*作为访问控制允许原点您也可以使用您的原点

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    res.header("Authorization", "Access-Control-Allow-Headers", "Origin","X-Requested-With", "Content-Type", "Accept");
    next();
});
更新

如果上述方法不起作用,请尝试以下方法,这将暂时启用所有功能

app.use(cors());

问题是Nginx不接受大文件。将其放在我的nginx服务器配置的位置块中解决了我的问题:
client\u max\u body\u size 10M

我执行此操作时收到
飞行前响应不成功
错误。从浏览器?是的,从手机browser@Vlady更新启用了所有功能,如果不起作用,我们可以放心地认为问题出在其他地方。这是我的初始设置。无论如何谢谢卡拉那首先想到的是:你在这两种情况下都使用https吗?在这两种情况下,您是以相同的方式登录还是注销?