Peters School of Business · Assiniboine College · NETW-0014
2 hours · Lecture + Lab 4C · Install, server block, self-signed cert, DH params, snippet config
DNS verified — now build the web server it points to
Nginx architecture, server blocks, SSL/TLS fundamentals, the snippet approach
Part 1 — Nginx as a web server (8 min)
/etc/nginx/ is the config root. The main config is nginx.conf — rarely edited directly. The sites-available / sites-enabled pattern: virtual host configs are written in sites-available/ and activated by creating a symlink in sites-enabled/. This makes it easy to enable/disable sites without deleting config files./var/www/. Default site at /var/www/html/. Each virtual host gets its own subdirectory: /var/www/yourname.net/html/.Part 2 — Server block anatomy (8 min)
listen 80; — port to listen on. IPv4. Adding listen [::]:80; also covers IPv6.
root /var/www/yourname.net/html; — base directory for website files.
index index.html index.htm; — default document to serve when a directory is requested. Nginx tries each in order.
server_name yourname.net www.yourname.net; — which Host headers this block responds to. If a request arrives for a hostname not listed in any server block, Nginx uses the default server.
location / { try_files $uri $uri/ =404; } — for any request path, try to find the exact file, then a directory index, then return 404. The = before 404 is an exact code return, not a redirect.
access_log and error_log — per-site log files. Without these, all traffic goes to the global /var/log/nginx/access.log.
Part 3 — SSL/TLS and the self-signed certificate (10 min)
/etc/nginx/snippets/. Any virtual host can activate them with two include lines. This keeps maintenance simple — update the snippet once and all sites benefit.Part 4 — nginx -t (4 min)
sudo nginx -t tests the configuration for syntax errors without restarting or affecting the running server. It prints the file and line number for any error found. Always run nginx -t before systemctl restart nginx or systemctl reload nginx. A failed restart that silently leaves Nginx stopped is worse than catching the error first. Make this a habit: edit → nginx -t → restart if OK.Lab 4C — Install Nginx, virtual host, SSL certificate, DH parameters, HTTPS server block
Part 1 — Install and verify (5 min)
sudo apt install nginx. Check status: sudo systemctl status nginx. Test the default page: curl http://localhost. Should return the Nginx welcome HTML. Note the default document root: /var/www/html/.Part 2 — Create virtual host for yourname.net (20 min)
sudo mkdir -p /var/www/yourname.net/html
sudo nano /var/www/yourname.net/html/index.html
<h1>Welcome to yourname.net</h1>
sudo nano /etc/nginx/sites-available/yourname.net
server {
listen 80;
listen [::]:80;
root /var/www/yourname.net/html;
index index.html index.htm;
server_name yourname.net www.yourname.net;
access_log /var/log/nginx/yourname.net.access.log;
error_log /var/log/nginx/yourname.net.error.log;
location / {
try_files $uri $uri/ =404;
}
}
sudo ln -s /etc/nginx/sites-available/yourname.net /etc/nginx/sites-enabled/
sudo nginx -t — must show "syntax is ok" and "test is successful"
sudo systemctl reload nginx
curl http://yourname.net (from S1 if DNS resolves to localhost, or specify IP). Browse from Windows host to http://yourname.net.
Part 3 — Generate SSL certificate and DH parameters (15 min)
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/nginx-selfsigned.key \
-out /etc/ssl/certs/nginx-selfsigned.crt
-x509 (self-signed), -nodes (no passphrase), -days 365 (validity), -newkey rsa:2048 (generate new 2048-bit RSA key pair simultaneously).
sudo openssl dhparam -out /etc/nginx/dhparam.pem 4096 &
& sends it to the background. It takes 2–5 minutes. Do not skip this step — without it, the ssl-params.conf snippet will fail to load when referencing dhparam.pem.
Part 4 — Create SSL snippet files (15 min)
sudo nano /etc/nginx/snippets/self-signed.conf
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
sudo nano /etc/nginx/snippets/ssl-params.conf
ssl_protocols TLSv1.3; — only allow TLS 1.3 (disables older, weaker versions)
ssl_dhparam /etc/nginx/dhparam.pem; — use our DH parameter file
ssl_stapling on; — OCSP stapling (will warn for self-signed — acceptable in lab)
add_header X-Frame-Options DENY; — security headers preventing clickjacking
Part 5 — Add HTTPS server block (15 min)
ls -lh /etc/nginx/dhparam.pem. If the file exists and is non-zero, it's done. If it's still generating, wait./etc/nginx/sites-available/yourname.net. Add a new server block for HTTPS, and modify the existing port 80 block to redirect:
server {
listen 80;
listen [::]:80;
server_name yourname.net www.yourname.net;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
root /var/www/yourname.net/html;
index index.html index.htm;
server_name yourname.net www.yourname.net;
access_log /var/log/nginx/yourname.net.access.log;
error_log /var/log/nginx/yourname.net.error.log;
location / {
try_files $uri $uri/ =404;
}
}
sudo nginx -t
sudo systemctl restart nginx
Learning outcomes — by end of Day 3, students can…
Common issues and fixes
| Issue | Likely cause | Fix |
|---|---|---|
| nginx -t fails: "dhparam.pem: No such file" | DH parameter generation not yet complete or was run in foreground and interrupted | Check: ls -lh /etc/nginx/dhparam.pem. If missing or 0 bytes, re-run: sudo openssl dhparam -out /etc/nginx/dhparam.pem 4096 (allow 2–5 minutes) |
| Browser shows "Connection refused" instead of cert warning | Port 443 not open in UFW, or Nginx not listening on 443 | Check: sudo ss -tlnp | grep nginx. If port 443 missing, check nginx -t for errors in the HTTPS server block. Also: sudo ufw allow 443/tcp |
| http://yourname.net loads the site instead of redirecting | Port 80 server block still has root/location directives instead of just return 301 | The port 80 block must only have listen, server_name, and return 301. Remove all root, index, and location lines from it |
| Visiting http redirects but HTTPS shows 404 | Document root path wrong, or index.html missing | Check: ls /var/www/yourname.net/html/index.html. Verify root directive in HTTPS server block matches the actual path |