ServerHandbook live check: run log
------------------------------------------------------------------------------
Article https://serverhandbook.com/kb/nginx-server-blocks-ubuntu
Check scripts/live/checks/nginx-server-blocks-ubuntu.mjs (sha256 c58fc0ac263bdadc7b84a6b34cc7bbe25d04c02154af3f03c870cbea8110006b)
Ran at 2026-11-18T22:51:46Z (98 s)
Result PASS, all 43 claims held
Tested on
System Ubuntu 26.04.1 LTS
Kernel 7.0.0-43-generic
Package nginx 1.28.3-2ubuntu1.12
Package nginx-common 1.28.3-2ubuntu1.12
Package curl 8.18.0-1ubuntu2.8
Setup A fresh Ubuntu 26.04 server from Ubuntu’s cloud image with ufw on and Nginx installed, reached over SSH as a sudo user; each site requested from a second machine by name.
Every version above was read from the server at run time. Passwords used on the
throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test
------------------------------------------------------------------------------
What the steps changed on the server (recorded before the first step and after the last)
Packages added (0)
Packages upgraded (0)
Packages removed (0)
Services enabled at boot (0)
Services disabled (0)
Listening sockets opened (2)
tcp 0.0.0.0:443 nginx
tcp [::]:443 nginx
Listening sockets closed (0)
Files added in /etc (6)
/etc/nginx/sites-available/catch-all
/etc/nginx/sites-available/example.com
/etc/nginx/sites-available/example.net
/etc/nginx/sites-enabled/catch-all
/etc/nginx/sites-enabled/example.com
/etc/nginx/sites-enabled/example.net
Files changed in /etc (0)
Files removed from /etc (1)
/etc/nginx/sites-enabled/default
Disk used on / (includes logs written during the run): +0.1 MB
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Check-your-server script (2 items): https://cdn.serverhandbook.com/check/nginx-server-blocks-ubuntu/ubuntu-26.04.1-20261118t225146z.sh
SHA-256 92225efcc79dcaaaf4ba1566ed9244de98e851e09e5e1bbe74ae1fef1f0a7b6d
------------------------------------------------------------------------------
On the stock server, before the first step:
FAIL a site of your own is turned on in sites-enabled
PASS the nginx config tests clean
After the last step:
PASS a site of your own is turned on in sites-enabled
PASS the nginx config tests clean
------------------------------------------------------------------------------
Claims (43), each with what the run saw
------------------------------------------------------------------------------
1. ok a bare Ubuntu 26.04 server box (web-server) starts
2. ok (setup) nginx installed and running, ufw on with SSH and Nginx Full allowed, as after the Install Nginx article
| Status: active
|
| To Action From
| -- ------ ----
| OpenSSH ALLOW Anywhere
| Nginx Full ALLOW Anywhere
| OpenSSH (v6) ALLOW Anywhere (v6)
| Nginx Full (v6) ALLOW Anywhere (v6)
3. ok (probe) before any server block, every name gets the default page from outside
| 200
4. ok tee prints each page it wrote
|
example.com
| example.net
5. ok the page belongs to root and is readable by everyone (-rw-r--r-- root root): no chown to www-data needed
| total 4
| -rw-r--r-- 1 root root 21 Nov 18 22:52 index.html
6. ok both server blocks are written (tee prints nothing with > /dev/null)
| |
7. ok sites-enabled holds three links: default, example.com, example.net, each pointing into sites-available
| total 0
| lrwxrwxrwx 1 root root 34 Nov 18 22:52 default -> /etc/nginx/sites-available/default
| lrwxrwxrwx 1 root root 38 Nov 18 22:52 example.com -> /etc/nginx/sites-available/example.com
| lrwxrwxrwx 1 root root 38 Nov 18 22:52 example.net -> /etc/nginx/sites-available/example.net
8. ok nginx -t is successful
| nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
| nginx: configuration file /etc/nginx/nginx.conf test is successful
9. ok reload prints nothing
10. ok each name gets its own site: example.com, example.net, and www.example.net the same as example.net
| example.com
| example.net
| example.net
11. ok (probe) from outside, through the firewall, each name gets its own page
| 200 example.com
| 200 example.net
12. ok the request for example.net is in example.net.access.log
| 203.0.113.1 - - [18/Nov/2026:22:52:59 +0000] "GET / HTTP/1.1" 200 21 "-" "curl/8.5.0"
13. ok (probe) the main access.log did not get the site requests
| 1
14. ok a name no block lists gets the default site ("Welcome to nginx!"): its listen line says default_server
| Welcome to nginx!
15. ok the default site is the one with "listen 80 default_server;"
| 22: listen 80 default_server;
| 23: listen [::]:80 default_server;
| 27: # listen 443 ssl default_server;
| 28: # listen [::]:443 ssl default_server;
16. ok with the default site off and no default_server anywhere, an unknown name gets example.com: the FIRST block in sites-enabled (alphabetical)
| example.com
17. ok (probe) from outside, a request by IP address also gets example.com
| 200 example.com
18. ok the catch-all block is written
19. ok nginx -t is successful: "listen 443 ssl" needs no certificate with ssl_reject_handshake on
| nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
| nginx: configuration file /etc/nginx/nginx.conf test is successful
20. ok an unknown name now gets no page at all: "curl: (52) Empty reply from server"
| curl: (52) Empty reply from server
21. ok over HTTPS the handshake is refused (no certificate is handed out): curl (35) … unrecognized name
| curl: (35) TLS connect error: error:0A000458:SSL routines::tlsv1 unrecognized name
22. ok example.com still answers
| example.com
23. ok (probe) from outside: by IP address no answer, by name the site
| 000 | 200 example.net
24. ok with its link removed and a reload, example.net falls to the catch-all (52 Empty reply)
| curl: (52) Empty reply from server
25. ok its config file is still in sites-available
| catch-all default example.com example.net
26. ok (turned on again, example.net answers)
27. ok (the config tests clean at the end of the steps)
| nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
| nginx: configuration file /etc/nginx/nginx.conf test is successful
28. ok (each site is a link into sites-available)
| total 0
| lrwxrwxrwx 1 root root 36 Nov 18 22:53 catch-all -> /etc/nginx/sites-available/catch-all
| lrwxrwxrwx 1 root root 38 Nov 18 22:52 example.com -> /etc/nginx/sites-available/example.com
| lrwxrwxrwx 1 root root 38 Nov 18 22:53 example.net -> /etc/nginx/sites-available/example.net
29. ok (tested on) the box reports its OS, kernel and the version of 3 package(s)
| PRETTY_NAME="Ubuntu 26.04.1 LTS"
| VIRT=none
| KERNEL=7.0.0-43-generic
| PKG curl 8.18.0-1ubuntu2.8
| PKG nginx 1.28.3-2ubuntu1.12
| PKG nginx-common 1.28.3-2ubuntu1.12
30. ok two default_server blocks on port 80: "[emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/…", test failed
| 2026/11/18 22:53:12 [emerg] 6933#6933: a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/default:22
| nginx: configuration file /etc/nginx/nginx.conf test failed
31. ok the same server_name in two files: only a [warn] "conflicting server name "example.com" on 0.0.0.0:80, ignored", and the test is SUCCESSFUL
| 2026/11/18 22:53:13 [warn] 7174#7174: conflicting server name "example.com" on 0.0.0.0:80, ignored
| 2026/11/18 22:53:13 [warn] 7174#7174: conflicting server name "www.example.com" on 0.0.0.0:80, ignored
| 2026/11/18 22:53:13 [warn] 7174#7174: conflicting server name "example.com" on [::]:80, ignored
| 2026/11/18 22:53:13 [warn] 7174#7174: conflicting server name "www.example.com" on [::]:80, ignored
| nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
| nginx: configuration file /etc/nginx/nginx.conf test is successful
32. ok (the first file read wins: example.com still serves its own page)
| example.com
33. ok ln with a relative path prints nothing (it does not check the target)
34. ok nginx -t: [emerg] open() "/etc/nginx/sites-enabled/example.net" failed (2: No such file or directory) in /etc/nginx/nginx.conf:NN
| 2026/11/18 22:53:15 [emerg] 7747#7747: open() "/etc/nginx/sites-enabled/example.net" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61
| nginx: configuration file /etc/nginx/nginx.conf test failed
35. ok the link points to sites-available/example.net INSIDE sites-enabled, which does not exist
| lrwxrwxrwx 1 root root 27 Nov 18 22:53 /etc/nginx/sites-enabled/example.net -> sites-available/example.net
36. ok a folder with no index.html: "HTTP/1.1 403 Forbidden"
| HTTP/1.1 403 Forbidden
| Server: nginx/1.28.3 (Ubuntu)
| Date: Wed, 18 Nov 2026 22:53:17 GMT
| Content-Type: text/html
| Content-Length: 162
| Connection: keep-alive
37. ok the site's error log: directory index of "/var/www/example.net/" is forbidden
| 2026/11/18 22:53:17 [error] 7997#7997: *18 directory index of "/var/www/example.net/" is forbidden, client: ::1, server: example.net, request: "HEAD / HTTP/1.1", host: "example.net"
38. ok root in /home/deploy/site: "HTTP/1.1 403 Forbidden"
| HTTP/1.1 403 Forbidden
| Server: nginx/1.28.3 (Ubuntu)
| Date: Wed, 18 Nov 2026 22:53:20 GMT
| Content-Type: text/html
| Content-Length: 162
| Connection: keep-alive
39. ok the error log: "/home/deploy/site/index.html" is forbidden (13: Permission denied)
| 2026/11/18 22:53:20 [error] 8563#8563: *19 "/home/deploy/site/index.html" is forbidden (13: Permission denied), client: ::1, server: example.net, request: "HEAD / HTTP/1.1", host: "example.net"
40. ok the home folder is drwxr-x--- (750): www-data may not enter it
| drwxr-x--- 5 deploy deploy 4096 Nov 18 22:53 /home/deploy
41. ok (back in /var/www, example.net answers again)
42. ok with both files gone the test is successful
| nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
| nginx: configuration file /etc/nginx/nginx.conf test is successful
43. ok its log files stay in /var/log/nginx until you delete them
| access.log error.log example.com.access.log example.com.error.log example.net.access.log example.net.error.log
------------------------------------------------------------------------------
Commands and what they printed (46)
------------------------------------------------------------------------------
$ sudo mkdir -p /var/www/example.com /var/www/example.net
$ echo 'example.com
' | sudo tee /var/www/example.com/index.html
example.com
$ echo 'example.net
' | sudo tee /var/www/example.net/index.html
example.net
$ ls -l /var/www/example.com
total 4
-rw-r--r-- 1 root root 21 Nov 18 22:52 index.html
$ sudo tee /etc/nginx/sites-available/example.com > /dev/null <<'EOF'
$ server {
$ listen 80;
$ listen [::]:80;
$ server_name example.com www.example.com;
$ root /var/www/example.com;
$ index index.html;
$ access_log /var/log/nginx/example.com.access.log;
$ error_log /var/log/nginx/example.com.error.log;
$ }
$ EOF
$ sudo tee /etc/nginx/sites-available/example.net > /dev/null <<'EOF'
$ server {
$ listen 80;
$ listen [::]:80;
$ server_name example.net www.example.net;
$ root /var/www/example.net;
$ index index.html;
$ access_log /var/log/nginx/example.net.access.log;
$ error_log /var/log/nginx/example.net.error.log;
$ }
$ EOF
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/
$ ls -l /etc/nginx/sites-enabled/
total 0
lrwxrwxrwx 1 root root 34 Nov 18 22:52 default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root 38 Nov 18 22:52 example.com -> /etc/nginx/sites-available/example.com
lrwxrwxrwx 1 root root 38 Nov 18 22:52 example.net -> /etc/nginx/sites-available/example.net
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl reload nginx
$ curl -H 'Host: example.com' http://localhost
example.com
$ curl -H 'Host: example.net' http://localhost
example.net
$ curl -H 'Host: www.example.net' http://localhost
example.net
$ sudo tail -n 1 /var/log/nginx/example.net.access.log
203.0.113.1 - - [18/Nov/2026:22:52:59 +0000] "GET / HTTP/1.1" 200 21 "-" "curl/8.5.0"
$ curl -s -H 'Host: other.example.org' http://localhost | grep ''
Welcome to nginx!
$ grep -n default_server /etc/nginx/sites-available/default
22: listen 80 default_server;
23: listen [::]:80 default_server;
27: # listen 443 ssl default_server;
28: # listen [::]:443 ssl default_server;
$ sudo rm /etc/nginx/sites-enabled/default
$ curl -H 'Host: other.example.org' http://localhost
example.com
$ sudo tee /etc/nginx/sites-available/catch-all > /dev/null <<'EOF'
$ server {
$ listen 80 default_server;
$ listen [::]:80 default_server;
$ listen 443 ssl default_server;
$ listen [::]:443 ssl default_server;
$ server_name _;
$ ssl_reject_handshake on;
$ return 444;
$ }
$ EOF
$ sudo ln -s /etc/nginx/sites-available/catch-all /etc/nginx/sites-enabled/
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl reload nginx
$ curl -H 'Host: other.example.org' http://localhost
curl: (52) Empty reply from server
$ curl -k https://localhost
curl: (35) TLS connect error: error:0A000458:SSL routines::tlsv1 unrecognized name
$ curl -H 'Host: example.com' http://localhost
example.com
$ sudo rm /etc/nginx/sites-enabled/example.net
$ curl -H 'Host: example.net' http://localhost
curl: (52) Empty reply from server
$ ls /etc/nginx/sites-available/
catch-all default example.com example.net
$ sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/
$ sudo nginx -t
2026/11/18 22:53:12 [emerg] 6933#6933: a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/default:22
nginx: configuration file /etc/nginx/nginx.conf test failed
$ sudo nginx -t
2026/11/18 22:53:13 [warn] 7174#7174: conflicting server name "example.com" on 0.0.0.0:80, ignored
2026/11/18 22:53:13 [warn] 7174#7174: conflicting server name "www.example.com" on 0.0.0.0:80, ignored
2026/11/18 22:53:13 [warn] 7174#7174: conflicting server name "example.com" on [::]:80, ignored
2026/11/18 22:53:13 [warn] 7174#7174: conflicting server name "www.example.com" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ cd /etc/nginx && sudo ln -s sites-available/example.net sites-enabled/
$ sudo nginx -t
2026/11/18 22:53:15 [emerg] 7747#7747: open() "/etc/nginx/sites-enabled/example.net" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61
nginx: configuration file /etc/nginx/nginx.conf test failed
$ ls -l /etc/nginx/sites-enabled/example.net
lrwxrwxrwx 1 root root 27 Nov 18 22:53 /etc/nginx/sites-enabled/example.net -> sites-available/example.net
$ curl -I -H 'Host: example.net' http://localhost
HTTP/1.1 403 Forbidden
Server: nginx/1.28.3 (Ubuntu)
Date: Wed, 18 Nov 2026 22:53:17 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
$ sudo tail -n 1 /var/log/nginx/example.net.error.log
2026/11/18 22:53:17 [error] 7997#7997: *18 directory index of "/var/www/example.net/" is forbidden, client: ::1, server: example.net, request: "HEAD / HTTP/1.1", host: "example.net"
$ mkdir ~/site && echo 'from home
' > ~/site/index.html
$ curl -I -H 'Host: example.net' http://localhost
HTTP/1.1 403 Forbidden
Server: nginx/1.28.3 (Ubuntu)
Date: Wed, 18 Nov 2026 22:53:20 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
$ sudo tail -n 1 /var/log/nginx/example.net.error.log
2026/11/18 22:53:20 [error] 8563#8563: *19 "/home/deploy/site/index.html" is forbidden (13: Permission denied), client: ::1, server: example.net, request: "HEAD / HTTP/1.1", host: "example.net"
$ ls -ld /home/deploy
drwxr-x--- 5 deploy deploy 4096 Nov 18 22:53 /home/deploy
$ sudo rm /etc/nginx/sites-enabled/example.net /etc/nginx/sites-available/example.net
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl reload nginx
$ sudo rm -r /var/www/example.net
$ ls /var/log/nginx/
access.log error.log example.com.access.log example.com.error.log example.net.access.log example.net.error.log
------------------------------------------------------------------------------
The check-your-server script, its full screen before the steps
------------------------------------------------------------------------------
ServerHandbook check, nginx-server-blocks-ubuntu (proved 2026-11-18)
FAIL a site of your own is turned on in sites-enabled
fix: https://serverhandbook.com/kb/nginx-server-blocks-ubuntu#step-3-enable
PASS the nginx config tests clean
1 passed, 1 failed
To fix a FAIL, follow the steps in https://serverhandbook.com/kb/nginx-server-blocks-ubuntu
------------------------------------------------------------------------------
The check-your-server script, its full screen after the steps
------------------------------------------------------------------------------
ServerHandbook check, nginx-server-blocks-ubuntu (proved 2026-11-18)
PASS a site of your own is turned on in sites-enabled
PASS the nginx config tests clean
2 passed, 0 failed
------------------------------------------------------------------------------
The check's source (scripts/live/checks/nginx-server-blocks-ubuntu.mjs)
As it ran.
------------------------------------------------------------------------------
/**
* KB: Nginx server blocks on Ubuntu 26.04 and 24.04 (run per release, --releases=26.04,24.04): two sites on one server (example.com, example.net), each with its own
* folder, config file and logs; turn them on, test before DNS (Host header); what a name that matches no block gets
* (the default site, then, with it removed, the FIRST block in sites-enabled); a catch-all for HTTP and HTTPS
* (return 444, ssl_reject_handshake); turn a site off; the mistakes and the errors they print: a duplicate default
* server, a conflicting server name, a broken symlink, 403 for a folder with no index, 403 for a site in a home folder.
*
* The server is a fresh VM box from Ubuntu's cloud image, reached as the sudo user `deploy`, as the Install Nginx
* article leaves it (hidden setup: ufw on with OpenSSH and 'Nginx Full' allowed, nginx installed). "From outside" is
* real: the VM's port 80 is forwarded to 127.0.0.1:PROBE_PORT on the laptop, and the probe sends the Host header a
* browser would send for that name.
*/
import { spawnSync } from 'node:child_process'
import { PROBE_PORT } from '../box/vm.mjs'
import { bareBox, reportBox } from '../lib.mjs'
export const meta = {
// The reader's check-your-server script (plan05 phase 3): proved by the runner on the stock server and after the steps.
reader: [
{ item: "a site of your own is turned on in sites-enabled", cmd: "ls /etc/nginx/sites-enabled/", expect: "^(?!default$)\\S+$", step: "step-3-enable" },
{ item: "the nginx config tests clean", cmd: "sudo nginx -t 2>&1", expect: "test is successful", step: "step-3-enable" },
],
}
const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04'
/** What a browser on another machine gets for http:///: [status, body] ('000' = no answer / empty reply). */
const outside = (name) => {
const r = spawnSync('curl', ['-s', '-m', '6', '-H', `Host: ${name}`, '-w', '\n%{http_code}', `http://127.0.0.1:${PROBE_PORT}/`], { encoding: 'utf8' })
const lines = r.stdout.split('\n')
return [lines.pop(), lines.join('\n').trim()]
}
const block = (name) => `server {
listen 80;
listen [::]:80;
server_name ${name} www.${name};
root /var/www/${name};
index index.html;
access_log /var/log/nginx/${name}.access.log;
error_log /var/log/nginx/${name}.error.log;
}`
const CATCH_ALL = `server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_reject_handshake on;
return 444;
}`
const tee = (path, body) => `sudo tee ${path} > /dev/null <<'EOF'\n${body}\nEOF`
const check = (ctx) =>
bareBox(ctx, async ({ sudoUser }) => {
const { run, L } = sudoUser()
L('sudo true')
// as the Install Nginx article leaves the server
L('sudo ufw allow OpenSSH && sudo ufw --force enable')
L('sudo apt-get update -q && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -q nginx', 900000)
L("sudo ufw allow 'Nginx Full'")
ctx.check('(setup) nginx installed and running, ufw on with SSH and Nginx Full allowed, as after the Install Nginx article',
L('systemctl is-active nginx') === 'active' && /Nginx Full/.test(L('sudo ufw status')), L('sudo ufw status'))
const [s0] = outside('example.com')
ctx.check('(probe) before any server block, every name gets the default page from outside', s0 === '200', s0)
// ── step 1: a folder and a page per site ─────────────────────────────────────────────────
run('sudo mkdir -p /var/www/example.com /var/www/example.net', { id: 'mkdir' })
const p1 = run("echo 'example.com
' | sudo tee /var/www/example.com/index.html", { id: 'page-com' })
const p2 = run("echo 'example.net
' | sudo tee /var/www/example.net/index.html", { id: 'page-net' })
ctx.check('tee prints each page it wrote', p1.out === 'example.com
' && p2.out === 'example.net
', `${p1.out} | ${p2.out}`)
const lsl = run('ls -l /var/www/example.com', { id: 'ls-page' })
ctx.check('the page belongs to root and is readable by everyone (-rw-r--r-- root root): no chown to www-data needed',
/^-rw-r--r-- 1 root root \d+ .* index\.html$/m.test(lsl.out), lsl.out)
// ── step 2: one config file per site ─────────────────────────────────────────────────────
const w1 = run(tee('/etc/nginx/sites-available/example.com', block('example.com')), { id: 'block-com' })
const w2 = run(tee('/etc/nginx/sites-available/example.net', block('example.net')), { id: 'block-net' })
ctx.check('both server blocks are written (tee prints nothing with > /dev/null)', w1.exit === 0 && w1.out === '' && w2.exit === 0 && w2.out === '', `${w1.out}|${w2.out}`)
// ── step 3: turn them on ─────────────────────────────────────────────────────────────────
run('sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/', { id: 'enable-com' })
run('sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/', { id: 'enable-net' })
const en = run('ls -l /etc/nginx/sites-enabled/', { id: 'ls-enabled' })
ctx.check('sites-enabled holds three links: default, example.com, example.net, each pointing into sites-available',
/default -> \/etc\/nginx\/sites-available\/default/.test(en.out) && /example\.com -> \/etc\/nginx\/sites-available\/example\.com/.test(en.out) && /example\.net -> \/etc\/nginx\/sites-available\/example\.net/.test(en.out), en.out)
const t1 = run('sudo nginx -t', { id: 'test-1' })
ctx.check('nginx -t is successful', t1.exit === 0 && /test is successful/.test(t1.out), t1.out)
const r1 = run('sudo systemctl reload nginx', { id: 'reload-1' })
ctx.check('reload prints nothing', r1.exit === 0 && r1.out === '', r1.out)
// ── step 4: test before DNS ──────────────────────────────────────────────────────────────
const c1 = run("curl -H 'Host: example.com' http://localhost", { id: 'curl-com' })
const c2 = run("curl -H 'Host: example.net' http://localhost", { id: 'curl-net' })
const c3 = run("curl -H 'Host: www.example.net' http://localhost", { id: 'curl-www' })
ctx.check('each name gets its own site: example.com, example.net, and www.example.net the same as example.net',
c1.out === 'example.com
' && c2.out === 'example.net
' && c3.out === 'example.net
', `${c1.out} | ${c2.out} | ${c3.out}`)
const [o1, b1] = outside('example.com')
const [o2, b2] = outside('example.net')
ctx.check('(probe) from outside, through the firewall, each name gets its own page', o1 === '200' && b1 === 'example.com
' && o2 === '200' && b2 === 'example.net
', `${o1} ${b1} | ${o2} ${b2}`)
// per-site logs
const lg = run('sudo tail -n 1 /var/log/nginx/example.net.access.log', { id: 'log-net' })
ctx.check('the request for example.net is in example.net.access.log', /"GET \/ HTTP\/1\.1" 200 /.test(lg.out), lg.out)
const lgc = L('sudo grep -c . /var/log/nginx/access.log || true')
ctx.check('(probe) the main access.log did not get the site requests', /^\d+$/.test(lgc) && Number(lgc) <= 1, lgc)
// ── a name that matches no block ─────────────────────────────────────────────────────────
const u1 = run("curl -s -H 'Host: other.example.org' http://localhost | grep ''", { id: 'unknown-default' })
ctx.check('a name no block lists gets the default site ("Welcome to nginx!"): its listen line says default_server',
u1.out === 'Welcome to nginx!
', u1.out)
const dg = run("grep -n default_server /etc/nginx/sites-available/default", { id: 'grep-default' })
ctx.check('the default site is the one with "listen 80 default_server;"', /listen 80 default_server;/.test(dg.out), dg.out)
run('sudo rm /etc/nginx/sites-enabled/default', { id: 'rm-default' })
L('sudo nginx -t && sudo systemctl reload nginx')
const u2 = run("curl -H 'Host: other.example.org' http://localhost", { id: 'unknown-first' })
ctx.check('with the default site off and no default_server anywhere, an unknown name gets example.com: the FIRST block in sites-enabled (alphabetical)',
u2.out === 'example.com
', u2.out)
const [o3, b3] = outside('203.0.113.10')
ctx.check('(probe) from outside, a request by IP address also gets example.com', o3 === '200' && b3 === 'example.com
', `${o3} ${b3}`)
// ── the catch-all ────────────────────────────────────────────────────────────────────────
const wc = run(tee('/etc/nginx/sites-available/catch-all', CATCH_ALL), { id: 'block-catch' })
ctx.check('the catch-all block is written', wc.exit === 0 && wc.out === '', wc.out)
run('sudo ln -s /etc/nginx/sites-available/catch-all /etc/nginx/sites-enabled/', { id: 'enable-catch' })
const t2 = run('sudo nginx -t', { id: 'test-catch' })
ctx.check('nginx -t is successful: "listen 443 ssl" needs no certificate with ssl_reject_handshake on', t2.exit === 0 && /test is successful/.test(t2.out), t2.out)
run('sudo systemctl reload nginx', { id: 'reload-catch' })
const u3 = run("curl -H 'Host: other.example.org' http://localhost", { id: 'unknown-444' })
ctx.check('an unknown name now gets no page at all: "curl: (52) Empty reply from server"', /curl: \(52\) Empty reply from server/.test(u3.out), u3.out)
const u4 = run('curl -k https://localhost', { id: 'unknown-tls' })
ctx.check('over HTTPS the handshake is refused (no certificate is handed out): curl (35) … unrecognized name',
/curl: \(35\)/.test(u4.out) && /unrecognized name/i.test(u4.out), u4.out)
const c4 = run("curl -H 'Host: example.com' http://localhost", { id: 'curl-com-2' })
ctx.check('example.com still answers', c4.out === 'example.com
', c4.out)
const [o4] = outside('203.0.113.10')
const [o5, b5] = outside('example.net')
ctx.check('(probe) from outside: by IP address no answer, by name the site', o4 === '000' && o5 === '200' && b5 === 'example.net
', `${o4} | ${o5} ${b5}`)
// ── turn a site off ──────────────────────────────────────────────────────────────────────
run('sudo rm /etc/nginx/sites-enabled/example.net', { id: 'disable-net' })
L('sudo nginx -t && sudo systemctl reload nginx')
const d1 = run("curl -H 'Host: example.net' http://localhost", { id: 'curl-net-off' })
ctx.check('with its link removed and a reload, example.net falls to the catch-all (52 Empty reply)', /curl: \(52\) Empty reply from server/.test(d1.out), d1.out)
const d2 = run('ls /etc/nginx/sites-available/', { id: 'ls-available' })
ctx.check('its config file is still in sites-available', /example\.net/.test(d2.out), d2.out)
run('sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/', { id: 'enable-net-again' })
L('sudo nginx -t && sudo systemctl reload nginx')
ctx.check('(turned on again, example.net answers)', L("curl -s -H 'Host: example.net' http://localhost") === 'example.net
', '')
const tEnd = L('sudo nginx -t 2>&1')
ctx.check('(the config tests clean at the end of the steps)', /test is successful/.test(tEnd), tEnd, {
reader: { cmd: 'sudo nginx -t 2>&1', expect: 'test is successful' },
})
const lsEnd = L('ls -l /etc/nginx/sites-enabled/')
ctx.check('(each site is a link into sites-available)', /example\.com -> \/etc\/nginx\/sites-available\/example\.com/.test(lsEnd), lsEnd, {
reader: { cmd: 'ls -l /etc/nginx/sites-enabled/', expect: ' -> /etc/nginx/sites-available/' },
})
ctx.stepsDone() // the article's steps end here; the mistakes and the undo follow
reportBox(
ctx,
L,
`A fresh Ubuntu ${RELEASE} server from Ubuntu’s cloud image with ufw on and Nginx installed, reached over SSH as a sudo user; each site requested from a second machine by name.`,
['nginx', 'nginx-common', 'curl'],
)
// ── mistakes ─────────────────────────────────────────────────────────────────────────────
// 1. the default site turned back on beside the catch-all
L('sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/')
const m1 = run('sudo nginx -t', { id: 'err-duplicate' })
ctx.check('two default_server blocks on port 80: "[emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/…", test failed',
m1.exit !== 0 && /\[emerg\] \d+#\d+: a duplicate default server for 0\.0\.0\.0:80 in \/etc\/nginx\/sites-enabled\/\S+:\d+/.test(m1.out) && /test failed/.test(m1.out), m1.out)
L('sudo rm /etc/nginx/sites-enabled/default')
// 2. example.net made by copying example.com and forgetting server_name
L("sudo sed -i 's/server_name example.net www.example.net;/server_name example.com www.example.com;/' /etc/nginx/sites-available/example.net")
const m2 = run('sudo nginx -t', { id: 'err-conflict' })
ctx.check('the same server_name in two files: only a [warn] "conflicting server name "example.com" on 0.0.0.0:80, ignored", and the test is SUCCESSFUL',
m2.exit === 0 && /\[warn\] \d+#\d+: conflicting server name "example\.com" on 0\.0\.0\.0:80, ignored/.test(m2.out) && /test is successful/.test(m2.out), m2.out)
L('sudo systemctl reload nginx')
const m2c = L("curl -s -H 'Host: example.com' http://localhost")
ctx.check('(the first file read wins: example.com still serves its own page)', m2c === 'example.com
', m2c)
L("sudo sed -i '0,/server_name example.com www.example.com;/s//server_name example.net www.example.net;/' /etc/nginx/sites-available/example.net")
L('sudo nginx -t && sudo systemctl reload nginx')
// 3. the link made with a relative path
L('sudo rm /etc/nginx/sites-enabled/example.net')
const m3l = run('cd /etc/nginx && sudo ln -s sites-available/example.net sites-enabled/', { id: 'err-link-make' })
ctx.check('ln with a relative path prints nothing (it does not check the target)', m3l.exit === 0 && m3l.out === '', m3l.out)
const m3 = run('sudo nginx -t', { id: 'err-link' })
ctx.check('nginx -t: [emerg] open() "/etc/nginx/sites-enabled/example.net" failed (2: No such file or directory) in /etc/nginx/nginx.conf:NN',
m3.exit !== 0 && /\[emerg\] \d+#\d+: open\(\) "\/etc\/nginx\/sites-enabled\/example\.net" failed \(2: No such file or directory\) in \/etc\/nginx\/nginx\.conf:\d+/.test(m3.out), m3.out)
const m3s = run('ls -l /etc/nginx/sites-enabled/example.net', { id: 'err-link-ls' })
ctx.check('the link points to sites-available/example.net INSIDE sites-enabled, which does not exist', /example\.net -> sites-available\/example\.net$/.test(m3s.out), m3s.out)
L('sudo rm /etc/nginx/sites-enabled/example.net && sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/')
L('sudo nginx -t && sudo systemctl reload nginx')
// 4. no index.html in the folder
L('sudo mv /var/www/example.net/index.html /var/www/example.net/home.html')
const m4 = run("curl -I -H 'Host: example.net' http://localhost", { id: 'err-noindex' })
ctx.check('a folder with no index.html: "HTTP/1.1 403 Forbidden"', /^HTTP\/1\.1 403 Forbidden/.test(m4.out), m4.out)
const m4l = run('sudo tail -n 1 /var/log/nginx/example.net.error.log', { id: 'err-noindex-log' })
ctx.check('the site\'s error log: directory index of "/var/www/example.net/" is forbidden', /directory index of "\/var\/www\/example\.net\/" is forbidden/.test(m4l.out), m4l.out)
L('sudo mv /var/www/example.net/home.html /var/www/example.net/index.html')
// 5. the site in the sudo user's home folder
run('mkdir ~/site && echo \'from home
\' > ~/site/index.html', { id: 'home-make' })
L("sudo sed -i 's#root /var/www/example.net;#root /home/deploy/site;#' /etc/nginx/sites-available/example.net")
L('sudo nginx -t && sudo systemctl reload nginx')
const m5 = run("curl -I -H 'Host: example.net' http://localhost", { id: 'err-home' })
ctx.check('root in /home/deploy/site: "HTTP/1.1 403 Forbidden"', /^HTTP\/1\.1 403 Forbidden/.test(m5.out), m5.out)
const m5l = run('sudo tail -n 1 /var/log/nginx/example.net.error.log', { id: 'err-home-log' })
ctx.check('the error log: "/home/deploy/site/index.html" is forbidden (13: Permission denied)', /"\/home\/deploy\/site\/index\.html" is forbidden \(13: Permission denied\)/.test(m5l.out), m5l.out)
const m5d = run('ls -ld /home/deploy', { id: 'err-home-ls' })
ctx.check('the home folder is drwxr-x--- (750): www-data may not enter it', /^drwxr-x--- \d+ deploy deploy /.test(m5d.out), m5d.out)
L("sudo sed -i 's#root /home/deploy/site;#root /var/www/example.net;#' /etc/nginx/sites-available/example.net")
L('sudo nginx -t && sudo systemctl reload nginx')
ctx.check('(back in /var/www, example.net answers again)', L("curl -s -H 'Host: example.net' http://localhost") === 'example.net
', '')
// ── undo: remove a site completely ───────────────────────────────────────────────────────
run('sudo rm /etc/nginx/sites-enabled/example.net /etc/nginx/sites-available/example.net', { id: 'undo-rm' })
const ut = run('sudo nginx -t', { id: 'undo-test' })
ctx.check('with both files gone the test is successful', ut.exit === 0 && /test is successful/.test(ut.out), ut.out)
run('sudo systemctl reload nginx', { id: 'undo-reload' })
run('sudo rm -r /var/www/example.net', { id: 'undo-www' })
const ul = run('ls /var/log/nginx/', { id: 'undo-logs' })
ctx.check('its log files stay in /var/log/nginx until you delete them', /example\.net\.access\.log/.test(ul.out), ul.out)
})
export default check