brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: proxy_intercept_errors lets nginx show its own error page for a failing backend

Nginx
~/tips/tip-nginx-proxy-intercept-errors-error-page

proxy_intercept_errors on;

Replaces backend 502, 503 and 504 responses with a friendly page that nginx serves itself.

When the app behind nginx crashes, visitors get either nginx’s bare 502 page or whatever ugly error the backend produced. I prefer a short page that says the site is back soon.

location / {
    proxy_pass http://127.0.0.1:8081;
    proxy_intercept_errors on;
    error_page 502 503 504 /50x.html;
}
location = /50x.html {
    root /srv;
    internal;
}

error_page alone only covers errors nginx makes itself, like a refused connection. proxy_intercept_errors on also catches status codes the backend sends, so a 502 from the app shows your page too. The status code stays 502, which is right: monitoring and search engines still see the error. internal stops people from opening /50x.html directly.

Keep the page itself static, small and without assets that come from the broken app. A maintenance page that loads its CSS from the backend that is down looks worse than the default.

It only intercepts codes that have an error_page for them. A JSON 404 from the backend passes through untouched, which is what you want for an API. Be careful when you add 404 or 400 to the list on a location that also serves an API: clients then get your HTML instead of the JSON error body they expect.

Further reading: proxy_intercept_errors, error_page

Going deeper: Guide: fix a 502 Bad Gateway in Nginx Proxy Manager, with a checklist that finds the cause

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_redirect http://localhost:3000/ /app/; sub_filter 'http://127.0.0.1:8081/' 'https://example.com/app/'; All Nginx tips (50) All topics

Link