简体   繁体   中英

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.

Application port is 5000.

Dir structure:

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

My 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:

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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