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
?
- 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
2 Answers
Reset to default 10For 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
版权声明:本文标题:javascript - NGINX with Express.js & socket.io | Cannot GET - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744236306a2596570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论