How to Setup Nginx Server Blocks (Virtual hosts)

By: Sunil Kumar |  In: Server  |  Last Updated: 2018/07/26

If you are familiar with apache server we use virtual hosts to host multiple websites on the same server.

On the same way, Nginx uses server blocks to encapsulate the server configuration details and serve more than one websites on a single server.

For setting up server blocks you need to have Nginx installed on your server. If you haven’t installed Nginx, following articles cover the process step by step.

How to install Nginx on Ubuntu Server

For the demonstration purpose, we are setting up two domain with the Nginx server- domain1.com and domain2.com. Although we can create as many server blocks as we want.

Step 1: Setup your New Document directories

by default Nginx has one server block enabled. /var/www/html is the default directory which will be served by Nginx if client request does not match to any domain.

If we want to configure multiple domains we need to create a directory structure within /var/www for each of the domain.

Here we are configuring two different domains so we will create two different directories within /var/www. You can create these directories at any path. It is not necessary to create the directory at /var/www.

So first we will make the directory structure for each of our domain

sudo mkdir -p /var/www/domain1.com/html
sudo mkdir -p /var/www/domain2.com/html

NOTE: It is not necessary for you to create the directory/project within /var/www/. You can create your project your project anywhere you want.

Step 2: Sample pages for each domain

If you already a working project you don’t need to create the sample page as you can point the domain to your actual project.

If you don’t have any working project, create a sample HTML file to test the configuration done very well and the domain is pointing to the right directory.

sudo nano /var/www/domain1.com/html/index.html

And place the following HTML code into it

<html>
    <head>
        <h1>domain1.com server block</h1>
    </head>    
</html>

And save the file(ctrl+o)

Also create a similar file for domain2.com also

sudo nano /var/www/domain2.com/html/index.html

And put the following code into it

<html>
    <head>
        <h1>domain2.com server block</h1>
    </head>    
</html>

Step 3: Create server block file

As we have created our project directory structure we have to create server block files which will be configured to point the domain name to the project root directory.

Nginx contains a default server block called default which can be used as a reference to create new server block.

First, we will create the server block file for domain1.com and then replicate the file for domain2.com

To create the server block file copy the default server block file for domain1

sudo cp /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/domain1.com

Now open the file in terminal

sudo nano /etc/nginx/sites-enabled/domain1.com

This file will look like this

server {

listen 80 default_server;

listen [::]:80 default_server;

root /var/www/html;

index index.html index.htm index.nginx-debian.html;

server_name _;

location / {

try_files $uri $uri/ =404;

}

}

NOTE: Only one of our server blocks on the server can have the default_server option enabled.

This specify which block should serve a request if the requested domain does nto match with any of the server block.

So in this guide we are letting the default server block serve the request does not match any server block.

So we will remove this configuration from doamin1 and domain2 server blocks

Now we will point the document root(specified by the root) to the project root we createed previously.

server {

listen 80;

listen [::]:80;

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

index index.html index.htm index.nginx-debian.html;

server_name domain1.com www.doamin1.com;

location / {

try_files $uri $uri/ =404;

}

}

So we removed default_server form domain1.com configuration file.

This is all the basic configuration we need to run an url on Nginx serve.

Copy the same configuration for domain2 also and make the required changes.

Step 4: Restart Nginx

In order to avoid a possible hash bucket memory problem that can arise from adding additional server names, we will go ahead and adjust a single value within our /etc/nginx/nginx.conf file. Open the file now:

 sudo nano /etc/nginx/nginx.conf

And uncomment the line server_names_hash_bucket_size be removing # from it.

To check the Nginx config file syntex run

 sudo nginx -t

If everything OK restart the Nginx server to apply all the configuration changes

 sudo service nginx restart

Modify /etc/hosts file for domain testing

If you have created domain1.com and domain2.com for testing and we dont own these two domain we need to modify this file to temporarly test the Nginx site.

 sudo nano /etc/hosts

And add

127.0.0.1 domain1.com domain2.com

And save the file.

Step 5: Test both of your domain

Now as we have made all the configuration changes successfully we can test the domain by hitting them in the web browser.

When you hit http://domain1.com your response will be like this

And when you hit http://domain2.com response will be like

Now as you can see both the websites are working fine.

NOTE: If your are setting up Nginx server for a PHP based website, you need to know that Nginx does not recognize PHP and it pass the PHP request to the php-fpm. So you need to install php-fpm for running PHP website on Nginx server.

To install php-fpm

sudo apt-get install php5-fpm

Conclusion

So we have configured two different server block on our Nginx server for two different projects. You can configure as many block as you want on a single Nginx server.

Comments



Sunil Kumar


I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
Know More

Join more than 10,000 others Web Developers


%d bloggers like this: