This post is an extension to my previous post on Nginx where we learned how to set up SSL on Nginx and forward connections to tomcat which was running on 8080. This time we need to host another website on this same server let’s see how we can get it done. FYI i am using ubuntu 14.04 and if you are using a different version please make necessary adjustments in file paths but i believe it will be same in many linux distros.

Step One : Host entry and website folders

First of all we are going to install Nginx if it’s not installed on your server

For demo purpose we are going to set up 2 websites app1.jft.com and app2.jft.com and in order get these working on your local machine please map these domains in your host file. Open your host file as follows :

Let’s map these domain names to your host file

Now save this file and test if your mapping is effective or not. Open command line and use ping command to test. You must check that reply should come from localhost or IP address you mapped in your host file :

Now let’s get to the real part and in order to comply with convention we are going to put our websites in /var/www folder which is default for Apache as well. We are going to create one folder each for both websites inside /var/www which we can do as follows :

Ever wondered what “-p” switch is doing there 🙂 Well it creates subdirectories by itself.

Now we need to give access to these website content to the www-data user which is regular user and we can do that as follows :

Also make sure you haven’t messed up with permission of your web root directory which is /var/www. To be on safe side let’s set it again

if you are adventorous type try avoiding last step and you will know what i mean in above 2 commands 🙂

Step Two : Create websites

Let’s create home page for both of our websites. Let’s start with app1.jft.com

Paste the following content in the file

Let’s create home page for app2.jft.com as well

Paste the following content in the file

Step 3 : Time to map server blocks in Nginx

Just so you know Nginx is a very helpful web server and it provides us with a default configuration file which we will be using shamelessly for our needs. 🙂

Creating server block both app1.jft.com and app2.jft.com

Now let’s edit these files one by one and you only need to focus on uncommented part. Don’t worry about the commented portion

The uncommented portion should look like this

Now let’s make some changes in this file and point it to our app1 website. You need to decide in which file you wish to keep default_server which is used when a request comes to server and no name matches the available server blocks. This is how your file should look after change

Let’s edit the server block for app2.jft.com

After your changes your website should look like this

You should notice that i removed default_server entry from here because that’s already entered in app1 server block. If you put it in both places nginx won’t start and you will wonder why 🙂

Step four : Enable server blocks

Upto this point Nginx doesn’t know that we have created 2 websites because the only place it cares about is sites_enabled folder which is where all active websites are placed. Now let’s make Nginx work for our websites as well by creating symbolic link to our sites

One point you must note that default file is already present and that also contains default_server param which will create problems for us. We should move it somewhere else or delete it choice is yours. I prefer to move it to my home folder and keep it for later use.

Time to restart Nginx and see the magic

Now go ahead and hit app1.jft.com and app2.jft.com and you will see both website running from same server. Congrats.

Tip : While restarting if you face error which says something like “could not build the server_names_hash” then you can fix it as follows :

Now look for entry with name server_names_hash_bucket_size and set it’s value to 64 and your conf should look like this

Hope it helps

Reference : This post takes inspiration from this post of digital ocean which helped me.