Complete Guide of Deploy Multiple Nodejs Applications with Different Domains

Backgroudn

I'm doing my side project (called 'FSG') which contains 3 modules: UserService, AdminService and APIService.

These three modules will be three independent nodejs application (not sure if this is a good design, if not, please let me know). And each of them will be assigned a domain/sub-domain name and a port like the table I draw above. Port 80 is accessed by everybody, while 81 and 82 has limited access. (This could be configured on your AWS -> EC2 -> Security Groups)

Apart of the domain name and port number that will be used by visitors, these three nodejs applications will have their own port inside of the server. UserService will use port 3000, APIService will use 3001 and AdminService use 3002.


Preparation

  • Install Ngnix on my server (AWS, Linode, DG, etc)
  • Open the dashboard of domain name provider

Do it!

  • Configure sub-domain Create new A record with same IP address of my server. In 'Name' column, enter the sub-domain name.

  • Configure Nginx

In this step, I'm gonna tell my Nginx how to map the request from visitors to the nodejs application which runs on my server.

Firstly, go check nginx.conf (under /etc/nginx). This file contains all of configuration that you want to set to your Nginx server. And it has example of how to set proxy of subdomain.

You can find line like this:

include /etc/nginx/default.d/*.conf;

This is how conf file load other configuration files, I create a folder 'site-conf' under /etc/nginx. And create 3 conf file (UserService.conf, AdminService.conf and APIService.conf). And in nginx.conf I put

include /etc/nginx/site-conf/*.conf;

Now once the Nginx get started, it will read configuration from the three conf files that I created for my nodejs app.

Then, I'm gonna edit the conf file. Here's the example of UserService. It says:

Hey nginx! Forward the request to localhost:3000, whenever there's a request from domain.com:80

server {  
        listen 80;
        server_name domain.com;
        access_log /var/log/nginx/userService.log;
        location / {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://127.0.0.1:3000;
                proxy_redirect off;
        }
 }

For APIService (81, api.domain.com, http://localhost:3001) and AdminService (82, admin.domain.com, http://localhost:3002), just make change of 'servername', 'listen' and 'proxypass'. (don't forget the "http://" in proxy_pass)

Now goes to /etc/ngnix and run 'sudo nginx -t' to check if everything in conf file is validated. After everything works well, I restarted Nginx. And run all of my nodejs application on correct port (UserService-3000, APIService-3001, AdminService-3002).

[I use pm2 to manage my nodejs app]

Now it should works, when I visit 'http://api.domain.com:81', Nginx should take me to the node application that runs on 3001 of my server.

I didn't put screenshot of the other two modules since I've already deploy my code there. But all of them work properly like I planed before.

Alright, that's all! Hope it helps!