【发布时间】: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; }