简体   繁体   English

Nginx反向代理找不到Node.js socket.io GET 404

[英]Node.js socket.io cannot GET 404 not found with Nginx reverse proxy

So I'm trying to make a game in nodejs/socket.io but whenever I deploy it on a server with a reverse proxy it does not work, everything works fine locally but on a reverse proxy it gives 404 not found errors. 因此,我试图在nodejs / socket.io中制作游戏,但是每当我将其部署在具有反向代理的服务器上均无法正常工作时,一切都在本地正常运行,但在反向代理上会出现404 not found错误。

Application port is 5000. 应用端口为5000。

Dir structure: 目录结构:

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

My app.js: 我的app.js:

var express = require('express');
var app = express();
var serv = require('http').Server(app);
app.get('/node',function(req, res){
  res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
serv.listen(5000);
var SOCKET_LIST={};
var io = require('socket.io')(serv,{});

index.html: index.html的:

var socket = io();
Img.player.src='client/img/player.png'; //I have more like this

Nginx: Nginx的:

 location ~ ^/(node|socket\.io) {
 proxy_pass http://127.0.0.1:5000;
 proxy_set_header Host $host;
 proxy_set_header Origin http://$host; 
 proxy_http_version 1.1;
 proxy_cache_bypass $http_upgrade;
 proxy_set_header Upgrade $http_upgrade;
 proxy_set_header Connection $http_connection;
 sub_filter /node /;
 }

When I go to myIpAdress/node/ I get the index.html file and I can log in to the application so socket.io works, the thing that doesn't work are the links to external files 'myIpAdress/node/client/img/player.png' gives 404 error. 当我转到myIpAdress / node /时,我得到了index.html文件,并且可以登录到应用程序,以便socket.io可以正常工作,但无效的是外部文件'myIpAdress / node / client / img的链接/player.png'会显示404错误。

Any ideas what the path is to the client folder? 任何想法到客户端文件夹的路径是什么?

Try adding another location block like so: 尝试添加另一个位置块,如下所示:

location /client {
    alias /path/to/client;
    access_log off;
}

The above location block tells NGINX to respond to client requests for content in yourdomain.com/client/ by serving it from the local /path/to/client directory. 上面的位置块告诉NGINX通过从本地/ path / to / client目录提供内容来响应客户对yourdomain.com/client/中内容的请求。

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

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