I have a web which works fine on a root domain like mydomain.com
without any modification. But if I want to serve it as mydomain.com/app1
I I need to modify the source code in the backend and statics links (css, images, etc) in the html
app.get('/')
to app.get('/app1')
src="main.css"
to src="app1/main.css"
Should I always modify the application when I want to assign a domain/path using nginx?
https://github.com/jrichardsz/nodejs-static-pages/blob/master/server.js
app.get('/', function(req, res) {
// ejs render automatically looks in the views folder
res.render('index');
});
This is my nginx configuration which works for mydomain.com
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://localhost:8080/;
}
}
server {
listen 80;
server_name mydomain.com;
location /app1/ {
proxy_pass http://localhost:8080/app1/;
}
}
And this is the fix in node js app
app.get('/app1', function(req, res) {
// ejs render automatically looks in the views folder
res.render('index');
});
I tried :
https://github.com/expressjs/express-namespace
http://expressjs.com/en/4x/api.html
But in both cases, I need change my node js app.
Thanks in advance.
Should you always modify the application when you want to assign a domain/path?
No, you shouldn't have to modify the application at all.
When you use proxy_pass
in this manner, you need to rewrite the URL with regex. Try something like this:
location ~ ^/app1/(.*)$ {
proxy_pass http://localhost:8080/$1$is_args$args;
}
See also: https://serverfault.com/q/562756/52951
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With