Reputation: 662
I currently have a digital ocean droplet connected to a domain. On the server, I'm running NGINX and trying to reverse proxy multiple node apps to it. Currently, my root directory has one node express app, at location / .
I'm trying to connect another node express application, to another sub directory. Here's the nginx config file:
server {
listen 80;
server_name servername.com;
# my root app
location / {
proxy_pass http://127.0.0.1:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# new app
location ~^ /newapp {
proxy_pass http://127.0.0.1:6002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
The problem is that the new app is trying to serve files out of /newapp, which is breaking. I'm thinking it's probably something in my app.js file to play around with Express in the new app, to set the base directory as /newapp/ - to serve both static files and routes from there. Any ideas on how to do this?
In the newapp, I'm serving static files as such:
// Serve files out of ./public
app.use(express.static(__dirname + '/public'));
and have route files as:
var index = require('./routes/index');
app.use('/', index);
with index routing file:
var express = require('express');
var router = express.Router();
// Get index page
router.get('/', function(req, res, next) {
res.render('index', {
index : 'active'
});
});
module.exports = router;
Upvotes: 2
Views: 2635
Reputation: 11
I have faced the same issue: Multiple node apps with NGINX in sub-directories. I used the code below. It is working fine.
location /smtest {
rewrite ^/smtest/(.*) /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:1234/;
}
Upvotes: 0
Reputation: 4445
First of all, don't use regexp location if you no need it. Use simple location. And about your question - put /
at the end of proxy_pass URI. Nginx will rewrite /newapp/xxx to /xxx and vice versa (for http redirects, for example). But (!) will not rewrite links in HTML body.
location /newapp/ {
proxy_pass http://127.0.0.1:6002/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
Upvotes: 5