jbel406 jbel406 - 3 months ago 27
Node.js Question

Multiple node.js projects with one domain name, each using path '/' as base url. Nginx & Express.js

I am successfully able to reverse proxy multiple node.js projects to different ports as in the Nginx set up below. What I am trying to accomplish however, is viewing the url as the root when it gets to the Node.js server.

For example, When someone goes to mydomain.com/projects/music_player, is it possible to have the Express application view the request url as just '/' instead of '/projects/music_player'.

My current Nginx setup, and Express configuration example are as follows.

Nginx:

server {
listen 80;

server_name mydomain_name.com;

location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

location /projects/music_player/ {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}


I want to do this:

app.get("/", function (req, res) {
res.sendFile("index.html");
});

app.listen(8000);


Instead of this:

app.get("/projects/music_player", function (req, res) {
res.sendFile("index.html");
});

app.listen(8000);


I am not sure if that is even possible. The reason is that I would like each of my node node.js/express applications to be deployable as standalone applications, without restructuring the code. I am trying to avoid having a bunch of domain names for each project, plus I think it would be pretty cool.

Answer

Yes! Of course you can.

I think the best way to do this is to rewrite your entire program into one express app, but you say you want to avoid this.

Then I think the short way with your current setup is to make each program listen to a different port and then map routes to them in your nginx.

For example:

dog.js

app.get("/", function (req, res) {
  res.sendFile("dog.html");
});

app.listen(8001);

cat.js

app.get("/", function (req, res) {
  res.sendFile("cat.html");
});

app.listen(8002);

And finally in your nginx config file:

server {
    listen 80;

    server_name mydomain_name.com;

    location / {
        proxy_pass http://localhost:8001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /cat/ {
        rewrite /cat/ / break;
        proxy_pass http://localhost:8002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Notice the rewrite directive, this prevents for the /cat/ location from being sent to your cat.js app that only expects the / route.