← Writing

Moving my site off Vercel.

Self-hosting this Next.js site behind Nginx: an ACME location that broke certificates, a Cloudflare redirect loop, a Next.js upgrade, and a sandboxed browser.

Sakshyam Baral··5 min readnginxinfra

This site used to live on Vercel, and Vercel was fine for it. It was a static-ish Next.js portfolio with a contact form. I moved it to my own server because the site was about to change shape. The new version reads its content from a small Go API with a SQLite database, has a private admin studio, and prints my CV to PDF on the server. All of that is easier when the site, the API and the database sit on one machine and talk over localhost.

The move itself is not complicated. Clone the repository, build it, run next start under systemd, and put Nginx in front of it with a certificate. Four things went wrong along the way, and each one is worth writing down.

The certificate that couldn't be issued

The server uses a hosting control panel that generates Nginx configs from templates. For a Next.js app you pick a proxy template that forwards requests to the app's local port. I made a custom template, a copy of the panel's Next.js one with my own upstream.

Let's Encrypt kept failing to issue the certificate. The HTTP challenge returned a 404.

The cause was a block in my template: its own location for the ACME challenge path. The panel answers ACME challenges with a regex location of its own, included from a separate file. My block was a ^~ prefix location, and a matching ^~ prefix tells Nginx to skip regex locations entirely. So mine won, it didn't serve the challenge files the panel had written, and every challenge was a 404.

Removing my block fixed it. The panel's own challenge handling was there all along. The rule I took from it: when a panel or a tool owns a path, a custom template must not also try to answer it, because the two will not agree on where the files are.

The redirect loop

DNS for the domain is on Cloudflare. Once the certificate was issued, I turned on Cloudflare's proxy for the record, and the site went into an endless redirect loop.

The SSL mode for the zone was Flexible. In that mode Cloudflare talks to the visitor over HTTPS but connects to the origin over plain HTTP. My Nginx config redirects every HTTP request to HTTPS. So Cloudflare asked for the page over HTTP, Nginx answered "go to HTTPS", Cloudflare passed that redirect to the browser, the browser asked Cloudflare for the HTTPS page again, and Cloudflare asked the origin over HTTP again.

The fix is to set the SSL mode to Full (strict) before turning the proxy on. Then Cloudflare connects to the origin over HTTPS and checks that its certificate is valid. The origin already had a real Let's Encrypt certificate, so strict mode worked straight away, and the force-HTTPS redirect stopped mattering for proxied traffic. Flexible mode is meant for origins that have no certificate at all. With a certificate on the origin, it is the wrong setting and it causes exactly this loop.

The order is what I would repeat: get the first certificate with the record unproxied, switch the SSL mode to Full (strict), and only then turn on the proxy.

Upgrading Next.js on the way

The site was on Next.js 13. While rebuilding it I moved to Next.js 14, on the same React version, and removed the content layer library that used to turn Markdown files into pages. Content now comes from the Go API, fetched on the server with time-based revalidation plus tag revalidation, so the API can refresh a page the moment I publish a change.

I don't build it on the server that serves it. Builds run on a separate build machine, and the result comes back as an archive that replaces the old build directory. The previous build is kept next to it, so a rollback is moving one directory back and restarting the service.

A browser that wouldn't start

The CV on this site is a PDF printed from the resume page by headless Chromium, so the PDF and the page never disagree. On this server, Chromium is installed as a snap.

The API runs as an unprivileged service user. When it started Chromium to print the page, Chromium refused to run. Snap confinement wants a writable folder in the user's home directory, and this user's home directory is owned by root. Snaps also get a private temp directory, so a PDF written to the usual temp folder never shows up where the caller looks for it.

Rather than change that home directory or run the API as root, I moved the printing to a small root-owned job, triggered by a file:

  • The API writes a request file in its data folder.
  • A systemd path unit watches that file. When it changes, it starts a one-shot service that runs as root.
  • That service checks its argument against a fixed list of environments, checks that the page it will print is on localhost, prints the PDF into a working folder the snap can write to, checks the output isn't empty, fixes the owner and permissions, and moves it into place.
  • The API waits for the PDF's modification time to change, with a timeout, and reports success or failure.

The root part is short enough to read in one screen. It takes nothing from the API except the fact that a file changed, and it can only print one page from a local address. That is a better trade than giving the browser or the API more rights than they need.

The site has been self-hosted since, behind Nginx and Cloudflare, with the API and the studio next to it.

Get new posts by email

One email when something new goes up. No tracking pixels, unsubscribe in one click.