小编Ker*_*eon的帖子

使用 socket.io/nginx/node 时出现 CORS 错误

我从 chrome 的控制台收到这个错误日志

XMLHttpRequest 无法加载https://subgroup.domain.com/socket.io/?EIO=3&transport=polling&t=Lpgp_mu。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:3000 '。响应的 HTTP 状态代码为 500。

我正在使用 Node.js、socket.io 在我的节点和 react.js 之间进行通信,使用 digitalocean 的 droplet 使用 nginx 来托管它们。

我已经阅读了很多关于 CORS 错误的信息,但我不确定在哪里修复错误。我一直试图在我的 NGINX 中允许它们

location /server { 
    proxy_pass http://localhost:8080;
    proxy_http_version 1.1;
    proxy_set_header Access-Control-Allow-Origin *;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
Run Code Online (Sandbox Code Playgroud)

从我的 node.js,服务器端:

var express = require("express");
var app = express();
var http = require("http");
var port = 8080;
var io = require("socket.io").listen(app.listen(port));

app.use("/", function (req, res, next) {
    res.status(200).send("Online …
Run Code Online (Sandbox Code Playgroud)

nginx node.js socket.io

9
推荐指数
1
解决办法
3819
查看次数

标签 统计

nginx ×1

node.js ×1

socket.io ×1