Summary – Host Two Web Apps on Same Server Same Port Different Domain Name Using NGINX.

| NGINX | Host Two Web Apps on Same Server Same Port Different Domain Name Using NGINX.

Two Steps:

  1. Start Your two or more Web Applications on two different internal ports.
  2. Setup NGINX Config to combine both the Webapps on same external port.

Procedure

Start Your two or more Web Applications on two different internal ports

TECH0002-2018.7.19-Host-Two-Webapps-on-Same-Server-Same-Port-Different-Domain-Name-Using-NGINX-1

One Webapplication listening at one internal port.

 

TECH0002-2018.7.19-Host-Two-Webapps-on-Same-Server-Same-Port-Different-Domain-Name-Using-NGINX-2

Other Webapplication listening at other internal port.

for example in NodeJS you need to append or change this line at the end of app.js file in your code

app.listen(9001, function(){
    console.log('Listening on 9001 Port');
});

Setup NGINX Config to combine both the Web Apps on same external port

You can install NGINX on windows using this tutorial.

TECH0002-2018.7.19-Host-Two-Webapps-on-Same-Server-Same-Port-Different-Domain-Name-Using-NGINX-4

Configure Nginx

Start Editing at Nginx > Conf > Nginx.conf :
  1. Create server block for each site.
  2. Server Blocks should be listenting at same port 80 or 443 but separate server_name.
  3. Each server block should proxify the respective webapplication port.
  4. Once done restart the Nginx Service.

The Nginx Code Looks Something Like This.

worker_processes 1;
events {
  worker_connections 1024;
}
http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
  #gzip on;
  server {
    listen 000.00.00.0:80 default_server;
    server_name avenue.in *.avenue.in;
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://127.0.0.1:9001;
    }
  }
  server {
    listen 000.00.00.0:80;
    server_name propertyshield.in *.propertyshield.in   propertyshield.tk *.propertyshield.tk;
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://127.0.0.1:9002;
    }
  }
}

Tadda done!!

TECH0002-2018.1.7-Host-Two-Webapps-on-Same-Server-Same-Port-Different-Domain-Name-Using-NGINX-0

Done

if you like the tutorial please spread the word aand comment below if you have any questions.