Julien Liabeuf

Install Nginx with Server Blocks

April 12, 2014 | 2 Minute Read | No Comments

One very important thing with the droplet I setup on DigitalOcean is to be able to host multiple sites. Hence, we're going to use Server Blocks (the equivalent of Virtual Hosts on Apache).

I found and tried several tutorials on how to create Server Blocks and I ended up creating my own "method" based on all the resources I found.

Ghost is the first thing I will host and in most tutorials I've been told to setup Node.js, Ghost and then Nginx. However, I found it better to start with Nginx and then do the rest.

Setup your Domain Hosts

First things first, make sure your domain is setup correctly. Wether you use the root domain or a subdomain, make sure the A record points to your server's IP address.

Create a New Directory

We now need to create the directory where the site will be hosted. In this example, I'll use domain.com as the domain.

mkdir /var/www/domain.com

Install Nginx

In your SSH terminal, type the following line to install Nginx

apt-get install nginx

Now let's delete the default configuration file, then we will create our own.

rm /etc/nginx/sites-available/default


Create a Server Blocks File

For the configuration file, I mostly based my settings on the Nginx documentation.

We can now create our configuration file.

sudo nano /etc/nginx/sites-available/blocks

I called it blocks in this example, but you can of course call it whatever you want.

This line will open the text editor in which we will input the custom configuration.


Create the Blocks

There are many ways of configuring a block. As of now I have only done it for Ghost, so here is what you need for it. However, if you refer to the Nginx documentation you will find all the basic elements for a block.

Note: When editing a file with nano, you can save the changes by pressing Ctrl+O (Ctrl right). Press Ctrl+X to exit.


server {
  server_name domain.com;
  access_log /var/log/nginx/domain.com.log;

  root /var/www/domain.com;
  index index.html index.htm;

  location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header HOST $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_redirect off;

Next step is to create a "symlink" to the directory sites-enabled. Indeed, sites-available only contains the available sites, it doesn't mean it's enabled.

sudo ln -s /etc/nginx/sites-available/blocks /etc/nginx/sites-enabled/blocks

Finally, let's restart Nginx.

service nginx restart


That's it. If everything went well, you should see the Nginx welcome page when browsing to domain.com.

Leave a Comment