Chapter 5

Nginx Basics

server blocks, root, index, proxy_pass.

Learning objectives

  • Read and write basic Nginx server blocks
  • Configure document root, index files, and redirects
  • Reverse-proxy to an upstream application when needed

Why Nginx for Workshop Co.

Nginx is a high-performance web server and reverse proxy. It handles static files (class photos, CSS) efficiently and forwards dynamic requests to PHP-FPM or other backends. Swift Host commonly provisions Nginx on Ubuntu for Canadian VPS customers like Workshop Co.

Minimal server block

A server block defines one virtual host — hostname, port, and where files live:

server {
    listen 80;
    server_name www.workshopco.ca workshopco.ca;

    root /var/www/workshopco/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}

root is the filesystem directory. A request for /classes looks for /var/www/workshopco/public/classes.

Common directives

DirectivePurpose
listenPort (80 HTTP, 443 HTTPS)
server_nameHostnames this block answers
rootDocument root path
indexDefault files for directory URLs
return 301Simple redirect without a file
proxy_passForward to another HTTP service

Worked example — apex to www redirect

Workshop Co. wants canonical URLs on www:

server {
    listen 443 ssl;
    server_name workshopco.ca;

    ssl_certificate     /etc/letsencrypt/live/workshopco.ca/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/workshopco.ca/privkey.pem;

    return 301 https://www.workshopco.ca$request_uri;
}

The main site block listens on www.workshopco.ca and serves content.

Reverse proxy snippet

If booking later moves to a Node app on port 3000:

location /api/ {
    proxy_pass http://127.0.0.1:3000/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}
Test before reload

Always run sudo nginx -t before sudo systemctl reload nginx. A syntax error in one site file can prevent Nginx from starting — taking every site on the box offline.

Try it yourself — static file

Workshop Co. adds a PDF price list at /downloads/2026-schedule.pdf. The file lives at /var/www/workshopco/public/downloads/2026-schedule.pdf. What URL path do visitors use, and which directive maps it?

Answer

URL: https://www.workshopco.ca/downloads/2026-schedule.pdf. Nginx maps it via root /var/www/workshopco/public — no extra location needed for a plain file.

Spot the mistake

server {
    listen 80;
    server_name workshopco.ca;
    root /var/www/workshopco/public;
}
server {
    listen 80;
    server_name workshopco.ca;
    root /var/www/staging/public;
}

What happens when a request arrives for workshopco.ca?

Answer

Duplicate server_name on the same listen socket — Nginx uses the first matching block. The second block is unreachable for that name. Use unique names or different ports.

Quick quiz

  1. What command validates Nginx config syntax?
  2. What does try_files $uri $uri/ /index.php?$query_string do?
  3. When would you use proxy_pass instead of root?
Answers
  1. sudo nginx -t
  2. Try the file, then directory, then fall back to index.php (front-controller pattern).
  3. When the app runs as a separate process (Node, Python, upstream API) rather than files on disk.