WittCode💻

Nginx sites-available and sites-enabled

By

Learn about the nginx sites-enabled and sites-available folders including sites-enabled vs sites-available, symbolic links or symlinks, the nginx listen, nginx root, nginx location directives, and the nginx server context.

Table of Contents 📖

sites-enabled

By default, the Ubuntu maintained nginx package comes with the folder sites-enabled inside /etc/nginx as a convention to imitate apache. This folder is used for getting a website up and running. If we look inside nginx.conf, we can see an include directive for /etc/nginx/sites-enabled/* inside the http context.

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

If we look inside the sites-enabled folder we can see a file titled default. The contents of this file is a server context.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
}

This server context describes an http server that serves a file on localhost port 80. The first directive to establish this functionality is the listen directive.

listen 80 default_server;
listen [::]:80 default_server;

The listen directive takes the form listen

:. Here, listen 80 makes nginx listen on all IPv4 addresses on port 80 and listen [::]:80 makes nginx listen on all IPv6 addresses on port 80. Adding the default_server option specifies this server context as the default server to respond to client requests. In other words, if the host in the request does not match a server_name, then it will default to this server block. Next is the root directive.

root /var/www/html;

The root directive specifies the root directory to search for a file. Here this is /var/www/html. If we check the presence of this folder, we can see a file called index.nginx-debian.html. The contents of the file are as follows.

<!DOCTYPE html>
<html>
    <head>
        <title>Welcome to nginx!</title>
        <style>
            body {
                width: 35em;
                margin: 0 auto;
                font-family: Tahoma, Verdana, Arial, sans-serif;
            }
        </style>
    </head>
    <body>
        <h1>Welcome to nginx!</h1>
        <p>If you see this page, the nginx web server is successfully installed and
        working. Further configuration is required.</p>

        <p>For online documentation and support please refer to
        <a href="http://nginx.org/">nginx.org</a>.<br/>
        Commercial support is available at
        <a href="http://nginx.com/">nginx.com</a>.</p>

        <p><em>Thank you for using nginx.</em></p>
    </body>
</html>

Next is the index directive. This directive defines the files that will be used as an index. One of these files is index.nginx-debian.html.

index index.html index.htm index.nginx-debian.html;

Next is the server_name directive. The server_name directive determines which server block is used for a client request. Here, setting it to _ is an invalid server name. In other words, this name will never match a request. However, this server block will act as a catch all because of the default_server specification.

server_name _;

Finally, the location directive routes requests to the specified location. Here, we are simply matching the root location. When nginx determines which server to handle the request, it then tests the URI from the request header against the parameters of the location directives.

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
}

Putting all this information together, sending a request to localhost:80 should return the index.nginx-debian.html file.

curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

sites-available

The sites-enabled folder determines which hosts are available in nginx. However, it works in tandem with the sites-available folder. The sites-enabled folder actually contains symlinks to files in the sites-available folder. A symlink is a symbolic link that points to another file/folder on a computer. We can check for the presence of a symlink by running the command ls -l.

ls -l
total 0
lrwxrwxrwx 1 root root 34 Jun 22 04:09 default -> /etc/nginx/sites-available/default

Therefore, the sites-available folder stores all virtual host configurations, whether or not they are currently enabled. The sites-enabled folder allows for the selective disabling of hosts by removing symlinks. For example, we could create a server context for wittcode.com in sites-available, but unless we create a symlink to the sites-enabled folder, it will not be accessible. If we remove the symlink for the default file, then localhost:80 will no longer serve the index file. We can remove a symlink with the sudo rm command.

sudo rm default

Now we need to reload nginx with sudo systemctl reload nginx.

sudo systemctl reload nginx

Now if we curl to localhost:80 we will get nothing returned.

curl localhost:80
curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused

Now lets recreate the symbolic link to default to get the site up and running again. We can create a symlink with the sudo ln -s where -s creates a symbolic link, source is the file we want to create a link to, and destination is where we want the link to be placed.

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

Now lets reload nginx again and curl localhost:80.

sudo systemctl reload nginx && curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>