We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – How To host Multiple Sites on Nginx with same Domain (FQDN)

Learn how to – How To host Multiple Sites on Nginx with same Domain (FQDN).

Introduction

“What is done wholeheartedly however small, is noble”

If you would wish to share the same FQDN such as api.ownyourlife.com.ng to serve more than one site or to serve APIs, then this will help you. If you have two sites hosted on different servers in your environment for example sales and accounting, your clients will need to do the following on the browser to access the different sites.

To Access Sales site: https://api.ownyourlife.com.ng/sales

To Access Accounts site: https://api.ownyourlife.com.ng/accounts

As you can see, we are using the same FQDN to serve different sites which are differentiated by /sales and /accounting. This will save you more public IPs and Firewall Rules if you have one in your environment.

We hope you have Nginx already installed and running.

### Install Nginx on Ubuntu / Debian ###
sudo apt -y install nginx
sudo systemctl enable --now nginx

### Install Nginx on CentOS / Fedora ###
sudo yum -y install nginx
sudo systemctl enable --now nginx

### Install Arch Linux / Manjaro ###
sudo pacman -S nginx
sudo systemctl enable --now nginx

Step 1: Modify Main Nginx Configuration file

Open up Nginx default configuration file and add the following line inside the http part. Make sure it is within the http curly brackets.

http {
....
......

include /etc/nginx/sites.d/*.conf;
}

This adds the configuration files in /etc/nginx/sites.d/ for nginx to read and act on them

Step 2: Create main Site configuration file

Let us now add our main site FQDN configuration file in /etc/nginx/sites.d

$ sudo vim api.learnhowto.conf

server {
  listen 80;
  server_name  api.ownyourlife.com.ng;
  return 301 https://api.ownyourlife.com.ng$request_uri;
}
 
server {
  listen 443 ssl http2;
  server_name api.ownyourlife.com.ng; 
  modsecurity_transaction_id "api.ownyourlife.com.ng-$request_id"; 
  access_log           /var/log/nginx/test-api-access.log; 
  error_log            /var/log/nginx/test-api-error.log; 
  include              /etc/nginx/default.d/api.ownyourlife.com.ng/*.conf;
}

The specific sites will be located in /etc/nginx/default.d/api.ownyourlife.com.ng/ and as you can notice, that has been included in our configuration file.

Step 3: Create site routing configurations

Now create the sites under /etc/nginx/default.d/api.ownyourlife.com.ng/ directory as included in the configuration above. In this setup, we’ll create one for sales

cd /etc/nginx/default.d/api.ownyourlife.com.ng

This configuration will redirect traffic to server 1 (192.168.55.60) hosting sales site listening on port 8443

$ sudo vim sales.conf
location /sales/ {
  proxy_http_version 1.1;
  proxy_pass https://192.168.55.60:8443/sales/;
  proxy_cache_bypass $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Upgrade $http_upgrade;
}

This configuration will redirect traffic to server 1 (192.168.55.63) hosting sales site listening on port 8080

 $ sudo vim accounts.conf 
location /accounts/ {
   proxy_http_version 1.1;
   proxy_pass https://192.168.55.63:8080/accounts/;
   proxy_cache_bypass $http_upgrade;
   proxy_set_header Connection 'upgrade';
   proxy_set_header Upgrade $http_upgrade;
}

That is all you need to configure your nginx to proxy your sites or APIs using the same FQDN. Thank you for reading through and before you decide to shift your eyes, take a look at related guides below:

Cart

Your Cart is Empty

Back To Shop