in Linux Nginx Server block Virtual host ~ read.

Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 14.04

Introduction

When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server.

In this guide, I will show configure server blocks in Nginx Web Server on an Ubuntu 14.04 server.

Step one – Change Document Root Directories

By default, Nginx on Ubuntu 14.04 has one server block enabled by default. It is configured to serve documents out of a directory at:

/usr/share/nginx/html

I won’t use the default since it is easier to work with things in the /var/www directory. So I will change document root directory to /var/www.

First, we need to create the necessary directories. We can do this with the following command. The -p flag tells mkdir to create any necessary parent directories along the way:

sudo mkdir -p /var/www/test.com/html  

Now that you have your directories created, we need to transfer ownership to our regular user. We can use the $USER environmental variable to substitute the user account that we are currently signed in on. This will allow us to create files in this directory without allowing our visitors to create content.

sudo chown -R $USER:$USER /var/www/test.com/html  

The permissions of our web roots should be correct already if you have not modified your umask value, but we can make sure by typing:

sudo chmod -R 755 /var/www  

Our directory structure is now configured and we can move on.

Step two – Create test page

Now that we have our directory structure set up, let’s create a default page for our sites so that we will have something to display.

Create an index.html file in your domain:

vim /var/www/test.com/html/index.html  

Inside the file, we’ll create a really basic file that indicates what site we are currently accessing. It will look like this:

  <title>Welcome to Test.com!</title>   
  <h1>Success! The test.com server block is working</h1>  

Save and close the file when you are finished. Now, you have a test page.

Step three – Create server block

By default, Nginx contains one server block called default which we can use as a template for our own configurations. We will begin by designing our server block and make the necessary modifications.

As mentioned above, we will create our server block config file by copying over the default file:

cd /etc/nginx/sites-available  
sudo cp default test.com  

Now, open the new file you created in your text editor with root privileges:

sudo vim /etc/nginx/sites-available/test.com  

Ignoring the commented lines, the file will look similar to 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; 
    } 
}

We are eventually going to disable the default server block configuration, so we can place the default_server option in either this server block or in the one for our other site. I’m going to leave the default_server option enabled in this server block, but you can choose whichever is best for your situation.

The next thing we’re going to have to adjust is the document root, specified by the root directive. Point it to the site’s document root that you created:

root /var/www/test.com/html;  

Note: Each Nginx statement must end with a semi-colon (;), so check each of your lines if you are running into problems.

Next, we want to modify the server_name to match requests for our first domain. We can additionally add any aliases that we want to match. We will add a www.test.com alias to demonstrate:

server_name test.com www.test.com;  

When you are finished, your file will look something like this:

server {  
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on; 
    root /var/www/test.com/html; 
    index index.html index.htm; 
    server_name test.com www.test.com; 
    location / { 
        try_files $uri $uri/ =404; 
    } 
}

That is all we need for a basic configuration. Save and close the file to exit.

Step four – Enable your server block

We can do this by creating symbolic links from these files to the sites-enabled directory, which Nginx reads from during startup.

We can create these links by typing:

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

This file are now in the enabled directory. However, the default server block file we used as a template is also enabled currently and will conflict with our file that has the default_server parameter set.

We can disable the default server block file by simply removing the symbolic link. It will still be available for reference in the sites-available directory, but it won’t be read by Nginx on startup:

sudo rm /etc/nginx/sites-enabled/default  

We also need to edit one setting really quickly in the default Nginx configuration file. Open it up by typing:

sudo vim /etc/nginx/nginx.conf  

We just need to uncomment one line. Find and remove the comment from this:

server_names_hash_bucket_size 64;  

Now, we are ready to restart Nginx to enable your changes. You can do that by typing:

sudo service nginx restart  

Nginx should now be serving your domain name.

Step five – Setup Local hosts file

You can edit hosts file by typing:

sudo vim /etc/hosts  

You need to add your domain local:

127.0.0.1 localhost  
127.0.0.1 guest-desktop  
127.0.0.1 test.com  

This will intercept any requests for test.com and send them to your server, which is what we want if we don’t actually own the domains that we are using.

Save and close the file when you are finished.

Step six – Test

Now that you are all set up, you should test that your server blocks are functioning correctly. You can do that by visiting the domains in your web browser:

http://test.com  

You should see a page with message "Success".

comments powered by Disqus