The /blog redirect that went back to http
Nginx behind an HTTPS proxy saw plain http and built a plaintext absolute redirect. One line, absolute_redirect off, makes it relative. Also, the SPA fallback that makes nonexistent pages answer 200.
/blog/ displayed fine. It was /blog , without the trailing slash, that broke. The response was a redirect to http://emilienpons.fr/blog/ , in plain http, while the whole site is served behind an HTTPS proxy. The browser followed the redirect and went back to http, where there is none anymore. Typing /blog/ directly brought the page up. The content was served. Only the path without the trailing slash failed.
For context, the site moved from a small homegrown CMS to a 100% static site. Angular with prerendering, about 160 pages, served by Nginx in a Docker container on a VPS, deployed by GitHub Actions. Between the browser and Nginx sits a proxy that terminates HTTPS and talks to Nginx over unencrypted HTTP. Serving static files is the easiest part of the deployment. The hard part is elsewhere: redirects, status codes, headers.
The scheme Nginx thinks it sees
Every prerendered page is a folder on disk with its own index.html . When Nginx receives a request for a folder without the trailing slash, it answers with a 301 to the URL with the slash. Normal, intended behavior, useful so that relative paths resolve the same way everywhere.
The trap is in how the destination URL gets built. By default, Nginx constructs an absolute address in the Location header, using the scheme of the connection as it sees it arrive. What it sees arriving is plain HTTP, because the proxy terminates TLS one level up. Nothing that reaches it by default tells it that, for the browser, that same connection is https. So it writes http:// , honestly.
curl showed it in one line:
$ curl -sI https://emilienpons.fr/blog | grep -i '^location'
Location: http://emilienpons.fr/blog/The browser follows that URL. What happens next depends on the proxy. The plaintext request can be refused. It can also be sent back to https, and the reader arrives after a detour. Either way, a plaintext URL has no business being there.
Only folder URLs without the slash go through that automatic redirect. A file served directly doesn't have the problem.
There are two families of fixes. The first tries to teach Nginx the real scheme from what the proxy forwards, the X-Forwarded-Proto header. That assumes a consistent configuration on both sides, and above all it only affects the redirects you write yourself. The slash redirect is one Nginx generates on its own, and it offers no variable where you could slip in the right scheme. Hence the second family, which asks Nginx to stop building absolute URLs altogether.
server {
listen 80;
server_name emilienpons.fr;
root /usr/share/nginx/html;
absolute_redirect off;
} The whole fix is one line: absolute_redirect off . The Location header becomes /blog/ . A relative redirect. The browser resolves it against the origin of the current page, https included. Relative redirects have been allowed by the HTTP spec for a long time, and every current browser handles them. This takes care of the port variant along the way too. Nginx can write the port it listens on into the absolute URL, and behind a proxy that's not the right one either. A relative redirect has neither scheme nor port to guess. The proxy can change, the container's listening port can change, the redirect stays correct because it no longer assumes anything about where it lands.
One annoying detail: the bug is invisible locally. Without a proxy in front, Nginx sees HTTP and writes HTTP, which is consistent, since everything runs in plaintext. It only shows up in the full chain.
The fallback that swallows 404s
The other line to watch when serving prerendered pages is the one everybody copies from SPA examples:
location / {
try_files $uri /index.html;
} For an SPA, that's correct. The routes live in the JavaScript bundle, the server has to return index.html everywhere and let the router decide. If the URL doesn't exist, the router shows its own 404 page.
For a prerendered site, it's wrong. Every page is a real file. A URL that matches no file points to a page that doesn't exist. With the SPA fallback, /whatever returns the homepage with a 200 status. A reader following a stale link sees the homepage, with no explanation. A link checker can no longer tell a page from an error, since everything answers 200. The version that fits:
location / {
try_files $uri $uri/ =404;
}$uri serves files. $uri/ handles folders, and triggers the redirect from the first part as a side effect. =404 returns a real status for everything else. If I want a decent error page, it gets added next to that with error_page , but the status has to stay 404.
The nuance is that the fallback still makes sense if some routes exist only on the client side, in the bundle. On this site, everything is prerendered. There is nothing to catch, so nothing to pass off as a page.
Three curls after a deploy
The deployment itself sees none of this. GitHub Actions builds, prerenders the pages, pushes the files. The build is green. Nothing in that chain looks at what Nginx does with the URLs the build doesn't produce.
Three curls cover both traps. Run them once the deploy is out.
curl -sI https://emilienpons.fr/blog | grep -i '^location'
curl -s -o /dev/null -w '%{http_code}\n' https://emilienpons.fr/
curl -s -o /dev/null -w '%{http_code}\n' https://emilienpons.fr/nonexistent-pageThe redirect must be relative. The home must answer 200. A made-up URL must answer 404. I'd add a page from the English version of the site to the list, since i18n doubles the opportunities to get a URL wrong. And for that one, look at the content, not just the status. I've had the English version prerendered in French, and it answered 200. A minute altogether.
The logical next step would be to put those curls into the workflow, so the deployment fails if a redirect goes back to http or an unknown URL answers 200.