admin管理员组

文章数量:1404063

I have a node/express.js/socket.io app setup on an Ubuntu Server running on port 3002. I've opened up all ports on the machine.

When I go to 11.111.111.1:3002/ everything works perfectly (both socket.io & express.js). I'm trying to proxy-pass the app to /mediaDev/ which seems to work in that I get an error message from express.js or node which says:

"Cannot GET /mediaDev"

Where as either not finding the node app returns 502 Bad Gateway.

But going directly to the app at my IP address plus port the app works perfectly just as expected.

Simplified NGINX:

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  server_name _;

  location ~ ^/mediaDev {
    proxy_pass http://localhost:3002;
  }
}

node.js related to serving content:

var app = express()
app.user(bodyParser.urlencoded({ extended:false}))
app.use(bodyParser.json())

app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')

app.get('/', function (req, res) {
  res.render('index')
})

var server = app.listen(PORT, function () {
  console.log("listening on port 3002")
})

var io = require('socket.io')(server, {
  path: '/mediaDev/socket.io'
})

Note: on the IP address both express.js and socket.io are working. On the actual domain express returned the error but my socket.io path /mediaDev/socket.io still works.

I thought the above was a bit strange. If I'm proxy passing /mediaDev and making my socket.io path /mediaDev/socket.io shouldn't I expect an error and that to actually be at /mediaDev/mediaDev/socket.io?

I have a node/express.js/socket.io app setup on an Ubuntu Server running on port 3002. I've opened up all ports on the machine.

When I go to 11.111.111.1:3002/ everything works perfectly (both socket.io & express.js). I'm trying to proxy-pass the app to /mediaDev/ which seems to work in that I get an error message from express.js or node which says:

"Cannot GET /mediaDev"

Where as either not finding the node app returns 502 Bad Gateway.

But going directly to the app at my IP address plus port the app works perfectly just as expected.

Simplified NGINX:

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  server_name _;

  location ~ ^/mediaDev {
    proxy_pass http://localhost:3002;
  }
}

node.js related to serving content:

var app = express()
app.user(bodyParser.urlencoded({ extended:false}))
app.use(bodyParser.json())

app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')

app.get('/', function (req, res) {
  res.render('index')
})

var server = app.listen(PORT, function () {
  console.log("listening on port 3002")
})

var io = require('socket.io')(server, {
  path: '/mediaDev/socket.io'
})

Note: on the IP address both express.js and socket.io are working. On the actual domain express returned the error but my socket.io path /mediaDev/socket.io still works.

I thought the above was a bit strange. If I'm proxy passing /mediaDev and making my socket.io path /mediaDev/socket.io shouldn't I expect an error and that to actually be at /mediaDev/mediaDev/socket.io?

Share Improve this question asked Dec 14, 2016 at 12:07 Philip KirkbridePhilip Kirkbride 22.9k39 gold badges131 silver badges237 bronze badges 2
  • Are you trying to proxy_pass the entire connection to port 3002 or just proxy socket.io? – jAndy Commented Dec 14, 2016 at 12:14
  • The entire connection – Philip Kirkbride Commented Dec 14, 2016 at 12:15
Add a ment  | 

2 Answers 2

Reset to default 10

For NGINX, it matters if you add a trailing slash to the URI (both in location and in proxy_pass). If you set it up properly, you can have it strip the prefix from the request path.

Try this:

location /mediaDev/ {
  proxy_pass http://localhost:3002/;
}

With this, a request to NGINX for /mediaDev/ will be "translated" to the request for "/" for Express. And it serves as a prefix, so /mediaDev/socket.io on the NGINX side will request /socket.io on the Express side.

Finding that doing the following gives me the desired behavior but if someone wants to give a better answer I feel this solution isn't ideal.

app.get('/mediaDev', function (req, res) {
  res.render('index')
})

This method seems to require me to hard-code the location I want to use in nginx into my node.js application. Ideally I'm hoping for a more modular solution.

本文标签: javascriptNGINX with Expressjs amp socketioCannot GETStack Overflow