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