NGINX Server Blocks: The Complete Guide

Updated: January 19, 2024 By: Guest Contributor Post a comment

Overview

NGINX is a powerful open-source web server that is used to host some of the largest and most visited sites on the internet. It is well known for its high performance, stability, rich feature set, and simple configuration. One of the most powerful features of NGINX is its ability to handle server blocks, which is a way to host multiple domains or websites on a single server. This concept is similar to virtual hosts in Apache. In this complete guide, we will delve into the world of NGINX server blocks, from understanding what they are to setting them up and troubleshooting common issues.

Understanding NGINX Server Blocks

Server blocks are used by NGINX to encapsulate configuration details and directives for each domain. By using server blocks, you can control several aspects of HTTP traffic on a per-domain basis, which includes site access, logging, and performance optimization settings. This is particularly useful when you are running multiple websites on a single server, as configurations for each website can be managed independently, without affecting others hosted on the same server.

Prerequisites

Before we start setting up NGINX server blocks, you will need:

  • A running instance of NGINX installed on your server.
  • Root or sudo privileges to the server.
  • Domain names pointing to your server’s public IP address.
  • Shell access to the server

Setting Up NGINX Server Blocks

The default configuration file for NGINX server blocks is usually located in /etc/nginx/sites-available/ directory. Server block files are then symlinked to the /etc/nginx/sites-enabled/ directory to be active and read by NGINX on start or reload.

sudo nano /etc/nginx/sites-available/example.com

Here’s an example of what a simple server block might look like for a domain:

server {
   listen 80;
   server_name example.com www.example.com;

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

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

Now create a symlink in the sites-enabled directory:

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

After setting up server blocks, you need to test the configuration for syntax errors:

sudo nginx -t

Customizing Server Blocks

You can add a variety of directives within your server block to customize the behavior of NGINX for each website.

location ~* \.php$ {
   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

location ~* \.htaccess {
   deny all;
}

Here we see a custom configuration for processing PHP files and a directive to deny access to .htaccess files for security.

SSL Configuration

NGINX also allows you to easily set up SSL certificates for server blocks, encrypting traffic between the server and client browsers. This is how you can modify the server block to listen on HTTPS.

server {
   listen 443 ssl;
   server_name example.com www.example.com;

   ssl_certificate /etc/ssl/certs/example.com.crt;
   ssl_certificate_key /etc/ssl/private/example.com.key;

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

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

Remeber to replace example.com.crt and example.com.key with the actual names of your SSL certificate and private key files, respectively.

Server Block Optimization

Optimization settings cache static files or configure upload sizes might be useful for improving performance.

server {
   #... existing configuration ...

   client_max_body_size 20M;

   location ~* \.js$ {
       expires 30d;
       add_header Cache-Control "public, must-revalidate, proxy-revalidate";
   }
}

Testing the Configuration

After making changes to your server blocks, you need to apply those changes by restarting NGINX:

sudo systemctl reload nginx

It’s vital to run nginx -t before reloading to make sure there are no syntax errors, which could result in downed server blocks or even the whole web server.

Troubleshooting Server Blocks

When something goes wrong with your server block configuration, NGINX will often refuse to restart. You can check the error log at /var/log/nginx/error.log which will generally give you a detailed description of the problem.

Conclusion

NGINX server blocks are a powerful tool for managing multiple websites on a single server smoothly. With the steps and tips provided in this guide, you should now have a solid understanding of how server blocks work in NGINX and how to configure them effectively to run your own sites with confidence.

As with any server configuration, always make sure to have backups and test configurations in a safe environment before applying them to your live sites. Happy hosting!