23.6. Operating Web Services

Configuration gets a service running; operations keep it honest. This section works the four routines that matter weekly: reading what happened, checking that it still works, changing what is deployed, and diagnosing the day something does not answer.

23.6.1. Access Logs, Error Logs, and Request Correlation

Every hop in this chapter's path writes its own record: Apache with the combined format, Nginx with the custom handbook format from Section 23.3.2, and the application with one line of its own. Make some traffic first. On client1, as john:

$ curl -sS https://shop.handbook.test/
<!doctype html>
<html lang="en"><head><meta charset="utf-8">
<title>Apache shop site</title></head>
<body><h1>shop.handbook.test</h1>
<p>Second name-based virtual host on the same address.</p>
</body></html>

Then read what the server recorded, on web1, as john:

$ sudo tail -n 3 /var/log/apache2/shop-access.log
192.168.234.20 - - [22/Nov/2026:17:03:30 +0000] "GET / HTTP/1.1" 200 459 "-" "curl/8.14.1"
192.168.234.20 - - [22/Nov/2026:17:03:30 +0000] "GET / HTTP/1.1" 200 459 "-" "curl/8.14.1"
192.168.234.20 - - [22/Nov/2026:17:09:09 +0000] "GET / HTTP/1.1" 200 4479 "-" "curl/8.14.1"

One line per request: client address, timestamp, request line, status, bytes, referrer, user agent. The per-vhost CustomLog from Section 23.2.2 is what keeps each site's traffic separable, and sudo tail is enough because these files need root - like all log access (Chapter 8 covers their rotation).

Correlating one request across two logs needs an identifier both sides share. The Nginx edge generates $request_id per request, forwards it as X-Request-ID, and logs it in its own format; the application echoes the same value into its log line. Extract the identifier from a live response, on client1, as john:

$ curl -sS -D - -o /dev/null https://app.handbook.test/api/echo | tr -d '\r' | awk 'tolower($1)=="x-request-id:" {print $2}'
a1f11c5ac3dd48b76f0db4fb83ca97fb

Three stages, one logical pipeline: -D - dumps the response headers to the terminal alongside the (discarded) body, tr -d '\r' strips the carriage returns HTTP lines end with, and awk prints the second field of the x-request-id header line, case-insensitively. Now follow that identifier into the edge's log, on web2, as john:

$ sudo grep -F 'a1f11c5ac3dd48b76f0db4fb83ca97fb' /var/log/nginx/app.access.log
192.168.234.20 - - [22/Nov/2026:17:09:13 +0000] "GET /api/echo HTTP/2.0" 200 279 "-" "curl/8.14.1" rid=a1f11c5ac3dd48b76f0db4fb83ca97fb

And into the application's own record, on app1, as john:

$ sudo grep -F 'a1f11c5ac3dd48b76f0db4fb83ca97fb' /var/log/handbook/app.log
2026-11-22T17:09:13Z rid=a1f11c5ac3dd48b76f0db4fb83ca97fb remote=192.168.234.20 path=/api/echo status=200 release=20261122-a

Same second, same client, same identifier, two logs on two hosts - and the application line adds what only the backend knows: the derived client address, the path, the status it returned, and the release that served it. When a user reports "one bad request at 14:05", this is the thread to pull: one identifier, every layer. Apache hosts get the same capability from mod_unique_id, whose UNIQUE_ID environment variable can be logged and forwarded the same way.

23.6.2. Health Checks, Deployment Changes, and Rollback

The health endpoint from Section 23.5.1 is the operational definition of "the service works": process up, database reachable, known release. Through the edge, on client1, as john:

$ curl -sS https://app.handbook.test/healthz
{"database": "ok", "release": "20261122-a", "status": "ok", "visits": 0}

Any monitor worth running - a systemd path check, a scheduled curl, an external prober - can test exactly this, and the release field means the check also fails loudly when a deployment quietly reverted.

Deployment is the symlink from Figure 23.4 changing target, plus a restart. The second release tree has existed since Section 23.5.1, holding the same code; what changes is which tree current names. Switch to it, on app1, as john:

$ sudo ln -sfn /srv/handbook/app/releases/20261122-b /srv/handbook/app/current
$ sudo systemctl restart handbook-app

Both are silent; the restart prints nothing and returns when the service is up again. Verify through the edge, on client1, as john:

$ curl -sS https://app.handbook.test/
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><title>Handbook application</title></head><body>
<h1>Handbook application</h1>
<p>release=20261122-b</p>
<p>client=192.168.234.20</p>
<p>scheme=https</p>
<p>host=app.handbook.test</p>
<p>request_id=61226b98ed795a1c7b9c707f0b08a82a</p>
</body></html>

Release 20261122-b is live. Rollback is the same operation with the old name, on app1, as john; both commands are again silent:

$ sudo ln -sfn /srv/handbook/app/releases/20261122-a /srv/handbook/app/current
$ sudo systemctl restart handbook-app
$ curl -sS https://app.handbook.test/
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><title>Handbook application</title></head><body>
<h1>Handbook application</h1>
<p>release=20261122-a</p>
<p>client=192.168.234.20</p>
<p>scheme=https</p>
<p>host=app.handbook.test</p>
<p>request_id=7e8a350d0f378193c86bd62b175f88a9</p>
</body></html>

The last block runs on client1, as john - the rollback check is a client-side question. Both releases stayed on disk throughout, which is the point of the layout: rolling back costs one symlink and one restart, and the previous release is still there for the post-mortem. Two operational notes: a simple service like this has no graceful reload - a restart aborts whatever requests it was holding, where the web servers of Sections 23.2.4 and 23.3.3 drain first - and the release directories should be cleaned only once nothing can plausibly roll back to them.

23.6.3. Application Configuration, Database Exports, and Restore Dependencies

Configuration lives in /etc/handbook/app.env, separated from the code so a release swap cannot change it by accident; it is mode 0640 root:handbook, and changing it requires the same restart as a release change. State lives in /var/lib/handbook/app.db. The export/restore exercise below is deliberately small, but the shape of every database recovery is already in it.

Check the tool version once, on app1, as john:

$ sqlite3 --version
3.46.1 2024-08-13 09:16:08 c9c2ab54ba1f5f46360f1b4f35d849cd3f080e6fc2b6c60e91b16c63f69aalt1 (64-bit)

Read the current counter, on app1, as john:

$ curl -sS http://127.0.0.1:8080/counter
visits=1 release=20261122-a

The request itself incremented the counter from zero and reported the new value. Export a consistent copy with SQLite's backup API - on app1, as john:

$ sudo -u handbook sqlite3 /var/lib/handbook/app.db ".backup '/var/lib/handbook/app-backup.db'"

The dot-command produces no output. .backup is not a file copy: the API walks the database under SQLite's own locking and writes a snapshot that is internally consistent even while the application keeps running - a raw cp of a live database can capture a half-written page instead. Stop the service before the restore, on app1, as john:

$ sudo systemctl stop handbook-app
$ sudo systemctl is-active handbook-app
inactive

The is-active check exits with status 3 when the unit is inactive - distinct from the exit 0 of the running checks earlier, and the detail a scripted restore step should test rather than parsing output.

Now damage the live database deliberately. The next command destroys a table in the service's database; on a real host it is the moment you confirm the backup exists and opens. On app1, as john:

$ sudo -u handbook sqlite3 /var/lib/handbook/app.db 'DROP TABLE visits;'

Silent, and the counter is gone. Restore the snapshot over it, on app1, as john:

$ sudo install -o handbook -g handbook -m 0640 /var/lib/handbook/app-backup.db /var/lib/handbook/app.db
$ sudo systemctl start handbook-app

Both silent; install sets owner, group, and mode in the same step the original file had. The service recreates its schema if a table is missing, so start succeeds either way - the proof is in the data. On app1, as john:

$ curl -sS http://127.0.0.1:8080/counter
visits=2 release=20261122-a

The counter was 1 in the backup, this request made it 2: the restored file is the one the service is using. Remove the practice backup, on app1, as john - like the other file operations it prints nothing:

$ sudo rm -f /var/lib/handbook/app-backup.db

The dependency ordering is the transferable part. The application must be stopped (or otherwise fenced off) while its database file is replaced; the replacement must restore ownership and mode; and the service must be started and asked, not assumed. With a real database the same skeleton holds with bigger tools: pg_dump/pg_restore for PostgreSQL, mariadb-dump for MariaDB, plus the service dependencies around them - Chapter 34 covers those procedures and their point-in-time variants, and Chapter 15 covers where the exported files should go and how long to keep them.

23.6.4. Diagnosing HTTP Errors, Timeouts, and Backend Failures

A client sees a status code; the operator needs the cause. The mapping for a reverse proxy is small enough to carry in your head:

Client sees Meaning Look first at
404 no such path application routing; try_files/vhost selection at the edge
403 access rule, or an untrusted forwarded header Directory/location rules; the application's trust list
413 request body over the limit client_max_body_size at the edge
502 the upstream connection failed backend unit state; the edge's error log
504 the upstream answered too slowly proxy_read_timeout; backend load or a hang

Walk them in order of how often they pay off. An unknown application path, through the edge, on client1, as john:

$ curl -sS -o /dev/null -w '%{http_code}\n' https://app.handbook.test/missing
404

The interesting failure is the proxy's own. Stop the backend, on app1, as john:

$ sudo systemctl stop handbook-app

And request the page again, on client1, as john:

$ curl -sS -o /dev/null -w '%{http_code}\n' https://app.handbook.test/
502

The edge answered - TLS, HTTP/2, headers, all normal - with the code that means "my upstream is unreachable". The reason is in the edge's error log, on web2, as john:

$ sudo grep -F 'connect() failed' /var/log/nginx/app.error.log | tail -n 2
2026/11/22 17:09:59 [error] 15647#15647: *114 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.234.20, server: app.handbook.test, request: "GET / HTTP/2.0", upstream: "http://192.168.234.12:8080/", host: "app.handbook.test"

Connection refused - the kernel answered the edge's connect because nothing held the port; errno 111 with the upstream address and the client's request in one line. Contrast that message with a timeout, where the log would say the connect or read timed out instead. Bring the backend back, on app1, as john:

$ sudo systemctl start handbook-app
$ curl -sS -o /dev/null -w '%{http_code}\n' https://app.handbook.test/
200

The recovery check runs on client1, as john - never declare recovery from the backend's own loopback alone. The timeout case is still on the shelf: the application sleeps five seconds on /slow, and the edge from Section 23.5.3 allows two. On client1, as john:

$ curl -sS -o /dev/null -w '%{http_code} %{time_total}\n' https://app.handbook.test/slow
504 2.019477

After almost exactly the configured two seconds the edge gave up and said 504. This is the one failure class where the client's timing is part of the diagnosis: 504 in two seconds says the edge's timeout fired; 504 in thirty says a longer limit somewhere else did. Fixing it is a decision, not a reflex - either the backend legitimately needs longer for that path (raise proxy_read_timeout for it, as the /ws location already does), or the backend is stuck and the timeout is doing its job by failing fast.

The same vocabulary transfers directly to Apache's mod_proxy: the 502/504 distinction, the per-site error log, and the shape of the "which layer answered" question are identical across both servers.