Setting up Server Blocks with Nginx


Nginx Server Blocks can be used to run host multiple websites on a single server or machine. The Nginx Server Blocks are very similar to the virtual hosts in Apache. The blocks allow you to use different SSL certificates for each site and you can specify the site document root directory (which contains the website files), you can create a separate security policy for each site, and much more.
In this document, we’ll discuss how to set up server blocks with Nginx on Ubuntu.


  • A domain name that points to your public server IP address.
    We will use
  • Nginx should be installed, up and running on the server.
  • Log in to the server with a non-root user with sudo privileges.

Process to Setup Server Blocks With Nginx

Creating Directory Structure

The website files are stored inside the document root and served as a response to the requests for the domain name. You can set any location of this document root.
Below is the example of the directory structure we are using:

As we want to host all of the domains on the server, we will create the directories for each of them inside /var/www directory, and then we will create a public_html directory that stores the files for the website.

Create the root directory for the domain e.g.​​​​​​​

Create an index.html file inside the document root directory of the domain for testing the site.

Insert the lines inside the index.html file

Command to change the permissions and ownership of files and directories from root user to Nginx user (www-data):

Creating Server Blocks

The Nginx configuration files for the server blocks are stored in /etc/nginx/sites-available directory by default and these are enabled through the symbolic links to the directory /etc/nginx/sites-enabled.

Create a new file e.g. inside /etc/nginx/sites-available directory

Paste below lines into this file:

Create a symbolic link from this new file of server block to enable it to the sites-enabled directory, which Nginx reads during startup.

Test the syntax of the Nginx configuration

If there is no error then the output should be:

Restart the services of Nginx

Open the browser of your choice and enter the URL e.g. to verify the configuration is working fine or not.
​​​​​​​The output should be like below:


In the above document, you have learned how to configure an Nginx server block to host more than one domain on a single Ubuntu server. You can repeat the above steps and create multiple server blocks for all your websites or domains.

Related Posts