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

sudo apt-get update
sudo apt-get install nginx

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 :

sudo vi /etc/hosts

Let’s map these domain names to your host file

127.0.0.1	app1.jft.com
127.0.0.1	app2.jft.com

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 :

gaurav@gaurav-Aspire-E1-572:/etc/nginx/sites-enabled$ ping app1.jft.com 
PING app1.jft.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.027 ms

gaurav@gaurav-Aspire-E1-572:/etc/nginx/sites-enabled$ ping app2.jft.com 
PING app1.jft.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.027 ms

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 :

sudo mkdir -p /var/www/app1.jft.com/html
sudo mkdir -p /var/www/app2.jft.com/html

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 :

sudo chown -R www-data:www-data /var/www/app1.jft.com/html
sudo chown -R www-data:www-data /var/www/app2.jft.com/html

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

sudo chmod -R 755 /var/www

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

vi /var/www/app1.jft.com/html/index.html

Paste the following content in the file

<html>
    <head>
        <title>Welcome to app1.jft.com!</title>
    </head>
    <body>
        <h1>Voila! app1.jft.com is working !</h1>
    </body>
</html>

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

vi /var/www/app2.jft.com/html/index.html

Paste the following content in the file

<html>
    <head>
        <title>Welcome to app2.jft.com!</title>
    </head>
    <body>
        <h1>Oh teri! app2.jft.com is also working !</h1>
    </body>
</html>

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

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/app1.jft.com

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/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

sudo vi /etc/nginx/sites-available/app1.jft.com

The uncommented portion should look like this

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }
}

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

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/app1.jft.com/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name app1.jft.com;
	location / {
        	try_files $uri $uri/ =404;
    	}
}

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

sudo vi /etc/nginx/sites-available/app2.jft.com

After your changes your website should look like this

server {
        listen 80
        listen [::]:80;

        root /var/www/app2.jft.com/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name app2.jft.com;
	location / {
        	try_files $uri $uri/ =404;
    	}
}

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

sudo ln -s /etc/nginx/sites-available/app1.jft.com /etc/nginx/sites-enabled/

sudo ln -s /etc/nginx/sites-available/app2.jft.com /etc/nginx/sites-enabled/

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.

sudo mv /etc/nginx/sites-enabled/default ~/default

Time to restart Nginx and see the magic

sudo service nginx restart

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 :

sudo nano /etc/nginx/nginx.conf

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

server_names_hash_bucket_size: 64;

Hope it helps

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