Linux Labs

Web stack / Stage 1 of 9

How nginx picks a site and a file

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 7 · on a machine · 7 min

A link enables the site

The docs block is written, but nginx does not read it. In this step you enable the site.

The main file, /etc/nginx/nginx.conf, pulls sites in with the line include /etc/nginx/sites-enabled/*;. On Ubuntu the site files live in /etc/nginx/sites-available/, and sites-enabled/ holds symbolic links to the ones that are enabled. To disable a site, delete the link: the file stays where it is.

nginx -T shows which names nginx actually reads: it checks the configuration and prints all of it, every included file too.

bash
sudo nginx -T | grep server_name

The only site name in the output is portal.lab.test; the lines with # are comments from nginx.conf. The docs file sits in sites-available, and sites-enabled has no link to it.

Check yourself 1 question

You created a link to the docs file in sites-enabled and ran curl http://docs.lab.test/ right away. What will it print?

  • A new link does not stop nginx: it keeps working with the old configuration and answers with the portal.

  • nginx -T will see the link: it reads the files from disk. The running nginx reads it on reload and answers the old way, with the portal, until then.

The running nginx read its configuration at startup and does not see the new link until it gets a reload. Until then docs.lab.test goes to the default server, the portal.

You know the order for applying a change from the lesson on service failures. First sudo nginx -t: if a file has an error, reload will refuse and nginx keeps its old configuration. Then sudo systemctl reload nginx — nginx rereads the files without dropping open connections. Last, a request to the site by name: a clean nginx -t only speaks about the syntax of the files, and only curl shows which block answers the site name.

Exercise · do it

Give the docs their name back

Make http://docs.lab.test/ open the docs. The portal must answer as before; leave the files in /srv/www alone.

Sign in and start The machine is available after signing in

Goals · 0 of 2

  • Open the docs by their name not done yet
  • Keep the portal's answer not done yet

Step goals

0 of 2
  • Open the docs by their name — not done yet
  • Keep the portal's answer — not done yet
Ubuntu 24.04

Step goals

0 of 2
  • Open the docs by their name — not done yet
  • Keep the portal's answer — not done yet
Loading…