Server

Why use the server directive?

Nginx will try the server blocks in the specified order. It uses the first block found with a listen and/or servername that satisfy the request.

The server block has contents which define:

  • the port(s) on which the server listens
  • the domain name(s) or IP addresses which the server should “capture”. This is particularly important if you are setting up virtual servers, or vanity URLs.
  • the root directory.

Wrinkles to watch for

Server blocks can appear split across multiple files. For example the default Debian/Ubuntu install places separate server blocks in separate files.

  • The server block selected depends on the “Host” field in the request. If there is no match the request will be routed to the default server for that port.
  • A default server may not be defined. In these cases, nginx uses the first defined server.

The interplay amongst multiple location blocks with plain text and regular expressions can be subtle.

How to troubleshoot?

Server blocks are one of the first blocks you have to get working. Proceed stepwise

  1. Make sure the server_name matching works properly.
  2. Any root directory is set correctly and has permissions allowing files to be readable by nginx.
    • If you need to check this it’s easy in a terminal session. Navigate to the root directory and type ls -l at the command prompt. This should show a non-root user. The default is www-data. If it shows root – then permissions need to be set with chown.

Then build up the location blocks step by step.

Insights from others

Watch this space

What directives are typically seen alongside this?

server_name

listen

location

root

Here’s the official text on the nginx site