7

I am completely stuck with a situation where I want to have several node applications on one server. I get this working fine by having the applications running on different ports. I can access the applications by putting in the ip address with port.

I would like to proxy the applications from my nginx server by using different sub-directories like so:

my.domain

location /app1 {
  proxy_pass http://10.131.6.181:3001;
}
location /app2 {
  proxy_pass http://10.131.6.181:3002;
}

Doing this I had to move the all the express routes to /app1 for application1. This works but now I am stuck with the static files.

I can now access the application with http://10.131.6.181:3001/app1 which is great, but via http://my.domain/app1 the static files are not loaded.

The static files can be accessed directly http://10.131.6.181:3001/css but not via the proxy http://my.domain/css

Ideally I would like to have the applications on different ports without the sub-directory in the express routes but only sub-directories in the proxy. I tried to put my head through the wall for the last 5 hours but didn't achieve anything.

Now I would happy if can at least get the static files via the nginx proxy.

7
  • you might want to take a look at serverfault.com/questions/562756/… , maybe this helps Commented Sep 28, 2016 at 18:14
  • do i need to setup nginx on the node server as well? so i redirect from the webserver to the node server and the redirect again in a second nginx on the node server? Commented Sep 28, 2016 at 18:28
  • ah, i thought this was nginx, do you use apache? Commented Sep 28, 2016 at 18:29
  • yes... i use nginx on my webserver and thought i can proxy straight to the node server but it doesn't work. Commented Sep 28, 2016 at 18:30
  • i tested the 302 return but it didn't work. Commented Sep 28, 2016 at 18:31

2 Answers 2

10

An updated answer for anyone who needs:

instead of

location /app1 {
  proxy_pass http://10.131.6.181:3001/app1;
}

use

location /app1/ {
  proxy_pass http://10.131.6.181:3001/;
}

or if on local

location /app1/ {
  proxy_pass http://localhost:3000/;
}

This is the correct way and this way you will not need to modify express. Express will receive only the part after /app1/

Sign up to request clarification or add additional context in comments.

1 Comment

It is. Sorry for the late change. I wasn't able to look into this before.
2

I finally worked it out after a google surge.

I added the directories to the nginx proxy_pass

my.domain

location /app1 {
  proxy_pass http://10.131.6.181:3001/app1;
}
location /app2 {
  proxy_pass http://10.131.6.181:3002/app2;
}

And I had to change the express applications to use the subdirectory

app.use('/app1', express.static(path.join(__dirname, 'public')));

app.use('/app1'', require('./routes'));

In the router I had to prefix all the redirects.

router.get('/logout', function (req, res) {
  req.logout();
  res.redirect('/app1/login');
});

The static files are called like so from html

<link rel="stylesheet" href="/app1/css/style.css"/>

A bit of a pain to change all the redirects and static url. I am sure there is a smarter way by setting a global variable in my node-express app. If anybody knows an easier way please post...

1 Comment

Did you ever find a better way? I'm at the same crossroads. New to Nginx, could adjust my entire express app, hoping for a simple fix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.