Ravindra BagaleCourses & study guides

7. Apache and Nginx: Install and Understand

7.5 Reading an Nginx Server Block

Config file vachta aali ki web server tumchya haatat yeto. Nginx madhe directives (naav value;) aani blocks (naav { ... }) astat.

main context  (user, worker_processes, error_log)
 ├── events { worker_connections ... }
 └── http {  (mime types, logging, gzip, include conf.d/*.conf)
        └── server {            ← one per website (a "server block")
               listen ...; server_name ...; root ...;
               └── location /path { ... }   ← rules for part of the URL
            }
     }

A fully commented server block:

# /etc/nginx/conf.d/mysite.conf   (Ubuntu: /etc/nginx/sites-available/mysite)
server {
    listen 80;                          # IPv4 port
    listen [::]:80;                     # IPv6 port
    server_name example.com www.example.com;   # which Host names this block answers

    root  /var/www/mysite;              # document root: URL "/" maps here
    index index.html index.htm;         # file served for a folder request

    location / {
        try_files $uri $uri/ =404;      # file? folder? otherwise 404
    }

    location /images/ {
        expires 7d;                     # let browsers cache images for 7 days
    }

    error_page 404 /404.html;           # custom error page (file inside root)
    autoindex off;                      # never list folder contents

    access_log /var/log/nginx/mysite_access.log;
    error_log  /var/log/nginx/mysite_error.log;
}
Directive What it does Example
listen Port (and optional IP) to accept connections on; default_server makes this the fallback listen 80 default_​server;
server_name Names matched against the Host header; _ is a "match nothing" placeholder server_​name example.​com www.​example.​com;
root Folder that URL paths are appended to root /​var/​www/​mysite;
index File(s) to try for a folder URL index index.​html index.​php;
location Rules for matching URL paths location /​api/ { ... }
try_files Try files in order, then fall back try_​files $uri $uri/ /​index.​html; (SPA)
return Send a redirect or status immediately return 301 https://$host$request_​uri;
error_page Custom error page error_​page 404 /​404.​html;
client_​max_​body_​size Maximum upload size (default 1 MB) client_​max_​body_​size 20M;
server_tokens Show/hide Nginx version in headers and error pages server_tokens off;

The two classic Nginx syntax errors

Every directive ends with a semicolon ;, and every { needs a matching }. sudo nginx -t prints the exact file and line number, e.g. unexpected "}" in /etc/nginx/conf.d/mysite.conf:9. Read it slowly. It is almost always the line before the one reported.

How Nginx chooses the server block

  1. It looks at blocks whose listen matches the port of the request.
  2. Among those, it picks the block whose server_name matches the Host header exactly (then wildcard names like *.example.com).
  3. If nothing matches, it uses the block marked default_server for that port, or the first block loaded if none is marked.

That rule explains the most common complaint: "I configured my site but I still see the welcome page." Some other block won, because it is the default or because your server_name doesn't match what the browser sent.

Ravindra Bagale's Tip

Forgetting a semicolon ; is the most common mistake students make in Nginx. sudo nginx -t tells you the file and line number – and the mistake is usually on the line before that line. Read calmly.

Practice task

Open /etc/nginx/nginx.conf on Amazon Linux (or /etc/nginx/sites-available/default on Ubuntu) and identify listen, server_name, root, index and every location block.