【问题标题】:Node.js socket.io/?EIO=3&transport=polling&t=LcuVjT6 404 (Not Found) reverse proxyNode.js socket.io/?EIO=3&transport=polling&t=LcuVjT6 404(未找到)反向代理
【发布时间】:2017-06-04 18:17:51
【问题描述】:

我正在尝试在服务器上部署 nodeJs 应用程序。

我在这样的 url 上得到 404:socket.io/?EIO=3&transport=polling&t=LcuVjT6 404 (Not Found) 以及 socket.io/?EIO=3&transport=polling&t=LcuVcaN 加载资源失败:服务器响应状态为 404(未找到)。

我的目录结构:

    ├── app.js
├── client
│   ├── css
│   │   └── main.css
│   ├── fonts
│   ├── img
│   ├── index.html
│   ├── index.pug
│   └── js
│       └── main.js
├── package.json

app.js

var express = require('express');
var app = express();
var serv = require('http').Server(app);

app.get('/',function(req, res){
  res.sendFile(__dirname + '/client/index.html');
});

app.use('/client',express.static(__dirname + '/client'));

serv.listen(8080);

var io = require('socket.io')(serv,{});

io.sockets.on('connection',function(socket){ //code }

index.pug

  ...
  script(src='https://cdn.socket.io/socket.io-1.4.5.js')
  script.
    var socket = io();
  ...

我尝试将端口更改为 8080 但没有帮助, servername:8080 上显示的唯一内容是 html 和 css 部分。看起来客户端无法加载socket.io js文件,我试图在我的pug文件的开头设置脚本但没有运气。

编辑:我的服务器在我家,当我浏览到 192.168.1.2:8080 时,它可以正常工作。我有一个从 127.0.0.1:8080 到 mywebsite.com/sevaho/nodegame/ 的反向代理,所以这就是问题所在。

nginx.conf

#REVERSE-PROXY APP 8080
     location ~ /sevaho/nodegame.* {
     rewrite (/nodegame)$ / break;
     rewrite /nodegame/(.*) /$1 break;
     proxy_pass http://127.0.0.1:8080;
     proxy_redirect / /nodegame/;
     proxy_set_header Host $host;
     proxy_set_header Origin http://$host; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection $http_connection;
     }

任何人都可以看到问题?

【问题讨论】:

  • 这是因为socket.io默认使用/socket.io路径,所以需要为/socket.io配置Nginx
  • 我按照你说的做了,改变了我的 nginx.conf 中的内容,但仍然无法正常工作。 #REVERSE-PROXY APP 8080 位置 ~ /socket.io.* { rewrite (/socket.io)$ / break;重写 /socket.io/(.*) /$1 中断; proxy_pass 127.0.0.1:8080; proxy_redirect / /socket.io/; proxy_set_header 主机 $host; proxy_set_header 来源 http://$host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header 升级 $http_upgrade; proxy_set_header 连接 $http_connection; }

标签: node.js socket.io


【解决方案1】:

它可能已经过时了,但您应该确保端口是免费且开放的。这是我在基本教程中遇到的问题,其中端口从 80(已打开并准备就绪)更改为 8080、4000 和其他可能性。如果不打开您使用的端口,您每次都会遇到这个问题。因此,当有人忘记您必须打开端口并意味着代码到目前为止无法工作时,请在更改代码之前先检查一下。在我的这个问题中,我用我找到的教程测试了所有可能性,此时它们都工作得很好。只是因为打开了正确的端口。 Node js and Socket.io Problems

【讨论】:

    猜你喜欢
    • 2017-02-25
    • 2017-09-04
    • 2020-11-24
    • 1970-01-01
    • 2021-07-09
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    相关资源
    最近更新 更多