简体   繁体   English

Nginx 和 NodeJS Express 的 CORS 问题

[英]CORS problem with Nginx and NodeJS Express

I have CORS problem that did not exist until today morning.我有直到今天早上才存在的 CORS 问题。 It happened probably after I updated my nginx on my Ubuntu server.它可能发生在我在我的 Ubuntu 服务器上更新我的 nginx 之后。 I've always had in my express server the following code:我的快递服务器中一直有以下代码:

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

And it worked perfectly.而且效果很好。 Now suddenly I get prerequest error, as you can see here.现在突然我收到预请求错误,如您在此处看到的。 The weird thing is - you need to refresh multiple times, eventually you'll get it.奇怪的是 - 你需要多次刷新,最终你会得到它。 It's like 20% of the time, and the other 80% it works fine.这就像 20% 的时间,另外 80% 的时间都很好。 https://my.treedis.com/public/asset/jtmDerKhYqV https://my.treedis.com/public/asset/jtmDerKhYqV

My nginx configuration is as follows:我的 nginx 配置如下:

      location / {
                proxy_pass http://localhost:5030;
                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;
}

For example firs time i tried to fetch and it failed due to CORS:例如,我第一次尝试获取但由于 CORS 而失败:

提取失败

And then another time I tried actually worked, same resource:然后我又一次尝试实际工作,相同的资源:

获取作品

I see Access-Control-Allow-Origin: * on your responses, when they work, but your socket.io thing is returning an invalid HTTP response more than half the time for me.我在您的回复上看到 Access-Control-Allow-Origin: * 当它们起作用时,但是您的 socket.io 事情在我的一半以上的时间里返回了无效的 HTTP 响应。

I suspect the "CORS error" you're seeing is on an HTTP error page, and not the resources you think you're serving up.我怀疑您看到的“CORS 错误”出现在 HTTP 错误页面上,而不是您认为正在提供的资源上。 Hit the "failing" URL directly and you'll see what I mean.... I get mostly 500 errors and this:直接点击“失败”URL,你就会明白我的意思了....我得到的主要是500个错误,这个:

Secure Connection Failed安全连接失败

An error occurred during a connection to api.treedis.com.连接到 api.treedis.com 时出错。 PR_END_OF_FILE_ERROR PR_END_OF_FILE_ERROR

The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.您尝试查看的页面无法显示,因为无法验证接收到的数据的真实性。 Please contact the website owners to inform them of this problem.请联系网站所有者告知他们这个问题。

The problem it seemed, was not related to CORS at all.这个问题似乎与 CORS 完全无关。 It was socket.io creating too many workers on nginx, which resulted in endless errors.是 socket.io 在 nginx 上创建了太多的 worker,导致了无穷无尽的错误。 This is why it worked for some times, but on other time it didn't.这就是为什么它在某些时候有效,但在其他时候却没有。 a look into /var/log/nginx/error.log gave me the solution, so I increased the workers to 20000 using worker_connections 20000;查看/var/log/nginx/error.log给了我解决方案,所以我使用worker_connections 20000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM