DEV Community

Cover image for Why is Nginx configuration organized as sites-available and sites-enabled?
Ganesh Kumar
Ganesh Kumar

Posted on

Why is Nginx configuration organized as sites-available and sites-enabled?

Hello, I'm Ganesh. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

In this article, We will exploring how to do nginx configuration directory structure. Why it is organized in this way?

How are ngix config files organized?

Majorly nginx config files are organized in the following way:

  1. Main config file: /etc/nginx/nginx.conf This deal with setting up global configuration for nginx.
  2. Sites available directory: /etc/nginx/sites-available This deal with setting up sites configuration for nginx. Where we can write multiple site configuration files.
  3. Sites enabled directory: /etc/nginx/sites-enabled This deal with enabling sites configuration for nginx. Where we can enable sites configuration.
  4. Logs directory: /var/log/nginx This deal with logs configuration for nginx. Where we can see logs of nginx.
  5. Cache directory: /var/cache/nginx This deal with cache configuration for nginx. Where we can see cache of nginx.

How to write site configuration file?

A site configuration file is a file that is used to configure a site for nginx.

Which you can further read in official documentation https://nginx.org/en/docs/.

These configuration should be written in this directory.

/etc/nginx/sites-available
Enter fullscreen mode Exit fullscreen mode

Once you write the configuration file. This will not enable the site.

How to enable the site?

To enable the site, you need to create a symbolic link from the sites-available directory to the sites-enabled directory.

For example, if you have a site configuration file named example.com in the sites-available directory, you can enable it by running the following command:

we should always test the configuration file before enabling it.

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Enter fullscreen mode Exit fullscreen mode

This will create a symbolic link from the sites-available directory to the sites-enabled directory.

The core reason for using symbolic link is that it will allow us to enable and disable the site without removing the configuration file.

if we directly put the configuration file in sites-enabled directory, then we need to remove the configuration file to disable the site.

That means if I want to update small change.
If the configration was copy to sites-enabled directory, then on nginx server restart, the changes will not be reflected.

so, to make it easier we use symbolic link.

Now once the site is enabled, we need to restart the nginx server to apply the changes.

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Finaly we can access the site in browser.

How to disable the site?

To disable the site, you need to remove the symbolic link from the sites-enabled directory.

sudo rm /etc/nginx/sites-enabled/example.com
Enter fullscreen mode Exit fullscreen mode

By this process it will not delete the configuration file. We can enable and disable the site without removing the configuration file.

Conclusion

By this we understood how to enable and disable the site in nginx.

How configuration file is organized in nginx.

git-lrc

Any feedback or contributors are welcome! It’s online, source-available, and ready for anyone to use.
⭐ Star it on GitHub: https://github.com/HexmosTech/git-lrc

Top comments (0)