尽管设置了标头,但CORS不起作用

Vla*_*nov 11 javascript nginx node.js cors

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

它适用于我的机器,但当其他人在不同的网络上尝试时会中断.给我这个错误:

[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.
Run Code Online (Sandbox Code Playgroud)

我在API上使用cors npm包,如下所示:

app.use(cors());
Run Code Online (Sandbox Code Playgroud)

所有这些都是通过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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我在计算机上的localhost上使用它时它工作得非常好,但是当我把它放在DigitalOcean上时我无法上传.当我上传文件时,它只在这个多部分请求中断,其他常规的Cors GET和POST请求都有效.

Vla*_*nov 8

问题是Nginx不接受大文件.把它放在我的nginx服务器配置的位置块中解决了我的问题:client_max_body_size 10M;