Friday, October 20, 2017

How To Configure Multiple Sub Domain in NGINX?

While creating my own Virtual Box CI/CD which include Jenkins, Artifactory and Git, I noticed that I need to have multiple ports for each application, so I decided to use NGINX to be my proxy server.

First, I set my DNS or in my local machine, I added an alias into my local (MacOS) /etc/hosts file

myadmin@myHostOS$: sudo nano /etc/hosts
# Add the following host aliases

192.168.1.101 interface101.vbox
192.168.1.101 artifactory.interface101.vbox
192.168.1.101 jenkins.interface101.vbox
192.168.1.101 git.interface101.vbox

# Save

I login to my Virtual Box instance that has the nginx in it.

You can have one file for all the configuration or you can have one for each sub-domain-configuration. I prefer one for each:


Step 1) Lets create the git configuration

admin@myGuestOS:$ sudo nano /etc/nginx/site-available/git-interface101-vbox.conf
# Add the following config
server {
  listen 80:
  server_name git.interface101.vbox;
  location / {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
     proxy_pass http://127.0.0.1:10112/; # Note: 10112 is the Git port
  }
}  
# Save

After saving the file, let's create a soft link in sites-enabled
admin@myGuestOS:$ ln -sf /etc/nginx/site-available/git-interface101-vbox.conf /etc/nginx/site-enabled/git-interface101-vbox.conf

Step 2) Lets create the artifactory configuration:

admin@myGuestOS:$ sudo nano /etc/nginx/site-available/artifactory-interface101-vbox.conf
# Add the following config
server {
  listen 80:
  server_name artifactory.interface101.vbox;
  location / {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
     proxy_pass http://127.0.0.1:8081/; # Note: 8081 is the Artifactory port
  }
}  
# Save 

After saving the file, let's create a soft link in sites-enabled
admin@myGuestOS:$ ln -sf /etc/nginx/site-available/artifactory-interface101-vbox.conf /etc/nginx/site-enabled/artifactory-interface101-vbox.conf

Step 3) Lets create the Jenkins configuration:

admin@myGuestOS:$ sudo nano /etc/nginx/site-available/jenkins-interface101-vbox.conf
# Add the following config
server {
  listen 80:
  server_name jenkins.interface101.vbox;
  location / {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
     proxy_pass http://127.0.0.1:8080/; # Note: 8080 is the Jenkins port
  }
}
# Save 

After saving the file, let's create a soft link in sites-enabled
admin@myGuestOS:$ ln -sf /etc/nginx/site-available/jenkins-interface101-vbox.conf /etc/nginx/site-enabled/jenkins-interface101-vbox.conf

Step 4) Lets restart the nginx service
admin@myGuestOS:$ sudo service nginx restart

Step 5) Lets verify the URL now.

From your Host OS, open a browser and visit the following URL:

http://artifactory.interface101.vbox for Artifactory
http://jenkins.interface101.vbox for Jenkins
http://git.interface101.vbox for Git

Note: Make sure Artifactory, Jenkins and Git are running before you visit their URL

Have fun and enjoy!

Cheers!