The existing posts I have read on this topic don’t have a working example. I have a simple file server where the root contains a 404.html that I want to be the body of the 404 response.
I already tried having /404.html at the end of my try files, but I don’t want to do that. I don’t want errors to have a 200 status code.
Some examples mention a status_code directive, but there is no status_code directive in v2. Maybe that was something that was considered that didn’t make it. Not sure.
I tried using respond and that didn’t seem to work either.
And v2.1 will likely have an even simpler error_page directive if all you want to do is serve a static file for an error page. But the handle_errors directive gives you full control over handling errors any way you want, whether it be a static file, a reverse proxy, a templated response, or whatever else.
I don’t fully understand what I have here. I just copied it from another forum post. This works, but not without file_server in the handle_errors block. That’s counterintuitive to me. It seems that root propagated into the handle_errors block, but not file_server.
That looks about right, for what is currently available. v2.1 will likely have some mapping directive and an error_page directive to make this a little simpler.
The root directive sets a variable that lives on the request context, and is valid for the duration of the request. So once invoked, it is remembered even through error routes.
You can get a sense for this if you look at the adapted JSON (showing only relevant excerpt):