Skip to content

3. Putting Nginx in Front, Then Automating All of It

Why a web server in front of the application

Section titled “Why a web server in front of the application”

After Lesson 2, the API and frontend were each exposed directly on their own ports — fine for a course VM, wrong for anything real: clients shouldn’t know which port the API listens on, static files shouldn’t be served by a Node process, and nothing was encrypted, including the checkout form. Nginx fixes all three: serves the React build straight from disk, reverse-proxies /api/ to Express so it’s never exposed directly, and terminates TLS.

The line that actually makes a single-page app work is try_files $uri $uri/ /index.html — there’s no file on disk called /cart; without that fallback, refreshing on any route but / returns a 404 before React’s router ever runs.

Self-signed first, then a real wildcard certificate

Section titled “Self-signed first, then a real wildcard certificate”

The course’s default is a self-signed cert, since a private-IP VM isn’t reachable from the internet and Let’s Encrypt can’t verify ownership of something it can’t see. Since christiehome.work is a real domain on Cloudflare, I skipped the standard HTTP-01 route entirely (which needs port 80 publicly reachable — a homelab VM behind NAT isn’t) and used Certbot’s Cloudflare DNS-01 plugin instead, proving domain ownership by writing a DNS record rather than serving a file publicly. That issued a real, trusted wildcard certificate, renewed automatically rather than needing manual regeneration every 365 days.

Everything up to this point — packages, database setup, cloning, configs, services — had been done by hand across two lessons. Repeat that after a VM dies and it’s the same afternoon lost twice. bootstrap.sh takes a fresh Ubuntu VM to a fully running, HTTPS-served Pretzel Shop in one pass, with set -euo pipefail so a failed step stops the script instead of continuing on a half-configured system.

Running it fresh surfaced two real bugs: a permission error writing .env (an earlier manual pass had left the directory root-owned), and the same Vite/plugin-react peer-dependency conflict from Lesson 1 — this time the fix (bumping the plugin version) got committed straight into the repo, so every future run on a new VM avoids the wall entirely instead of hitting it again.

Migrating from an IP to the real domain broke the API silently

Section titled “Migrating from an IP to the real domain broke the API silently”

Once the script pointed at pretzel.christiehome.work instead of a raw IP, the browser threw a certificate error — but only on API calls, not the page itself. The cause: the frontend’s compiled JavaScript still called the API at the old VM IP, because VITE_API_BASE_URL gets baked into the JS bundle at build time, not runtime. Nginx and DNS had moved on to the domain; the already-built frontend hadn’t. Fixed by rebuilding with the current value and redeploying — the same class of bug as an env value that’s correct for one origin silently breaking the moment the actual origin changes.

Certbot renewal failing silently is the one worth knowing: Let’s Encrypt certs expire every 90 days, and if renewal fails (DNS changed, credentials rotated), nothing surfaces until a user hits a certificate-expired error in their browser. certbot certificates, or an external monitor, catches it before a user does.


Next: Lesson 4, Docker and Compose.