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
| Directive | Purpose |
|---|---|
listen | Port (80 HTTP, 443 HTTPS) |
server_name | Hostnames this block answers |
root | Document root path |
index | Default files for directory URLs |
return 301 | Simple redirect without a file |
proxy_pass | Forward 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;
}
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
- What command validates Nginx config syntax?
- What does
try_files $uri $uri/ /index.php?$query_stringdo? - When would you use
proxy_passinstead ofroot?
Answers
sudo nginx -t- Try the file, then directory, then fall back to
index.php(front-controller pattern). - When the app runs as a separate process (Node, Python, upstream API) rather than files on disk.