Organization: Keeping each site's configuration in separate files makes the server easier to manage, especially when hosting multiple sites. You can easily enable, disable, or modify configurations for individual sites without affecting the others.
Scalability: As you host more websites, you won't clutter the main
nginx.conf
file with site-specific configurations. This keeps your main configuration file clean and focused on global settings.Ease of Enable/Disable: You can enable a site by creating a symbolic link in the
sites-enabled
directory to its configuration file insites-available
. Similarly, you can disable a site by removing the symbolic link. This setup allows for quick enabling and disabling of sites without actually removing their configuration files.
How Does It Work?
/etc/nginx/sites-available
: This directory stores the available server block configurations. Each site hosted on the server has its configuration file here, such asdomain.com.conf
. However, merely having a configuration file insites-available
does not activate the site./etc/nginx/sites-enabled
: This directory contains symbolic links to configuration files in thesites-available
directory. Nginx reads all the configurations linked here when starting up. Only sites linked in this directory are served by Nginx.
Creating and Enabling a New Site
To add a new site, you would:
Create a new configuration file in
/etc/nginx/sites-available/
, such asyour_domain.com.conf
, and define the server block for your site.Enable the site by creating a symbolic link in
/etc/nginx/sites-enabled
to the configuration file you just created insites-available
. You can do this with theln -s
command:bashsudo ln -s /etc/nginx/sites-available/your_domain.com.conf /etc/nginx/sites-enabled/
Test the Nginx configuration for errors:
bashsudo nginx -t
Reload Nginx to apply the changes:
bashsudo systemctl reload nginx