Nginx sites-available and sites-enabled on Debian

In Debian and Debian-based operating systems, Nginx uses a dual-directory layout consisting of sites-available and sites-enabled to manage virtual host configurations. This article provides a clear breakdown of how this structure works, why it is used, and how to effectively enable, disable, and manage server blocks within /etc/nginx/.

The Directory Structure

Debian places Nginx configuration files inside the /etc/nginx/ directory. Within this path, you will find two specific directories dedicated to managing virtual hosts:

  • /etc/nginx/sites-available/: This directory acts as a storage repository for all your server block configuration files, regardless of whether they are actively serving traffic.
  • /etc/nginx/sites-enabled/: This directory contains symbolic links (symlinks) pointing directly to the configuration files located in sites-available.

How Nginx Processes the Layout

The main configuration file, /etc/nginx/nginx.conf, dictates how Nginx behaves. Inside the http context of this file, Debian includes a directive:

include /etc/nginx/sites-enabled/*;

Because of this line, Nginx parses only the files found in sites-enabled when starting or reloading. It completely ignores files stored in sites-available unless they are linked to sites-enabled.

Enabling and Disabling Sites

This layout allows administrators to quickly toggle sites on or off without deleting configuration files or altering main settings.

Enabling a Site

To activate a configuration stored in sites-available, create a symbolic link targeting it inside sites-enabled:

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

Disabling a Site

To deactivate a site, remove the symbolic link from sites-enabled. The original configuration remains intact in sites-available for future use:

sudo rm /etc/nginx/sites-enabled/example.com

Applying Changes

Whenever you add, modify, or remove links, always verify the configuration syntax before applying the changes:

sudo nginx -t
sudo systemctl reload nginx

Why Debian Uses This System

The separation of available and enabled configurations offers several practical advantages:

  • Modularity: Keeps configurations cleanly separated into individual files rather than one unmanageable monolithic file.
  • Staging: Allows you to draft, test, and store new domain configurations before making them live.
  • Rapid Rollback: Enables you to take a compromised or malfunctioning site offline instantly by removing a single symlink, leaving the configuration available for troubleshooting.