Score:0

nginx - error_page is ignored when I throw an error from try_files

so flag

I have the following config:

error_page 502 =200 @proxy_down;
error_page 503 @maintenance;

location / {
    proxy_pass http://proxy;
    proxy_redirect off;
}

location @proxy_down {
    root /path/to/folder/;
    try_files $uri =503;
}

location @maintenance {
    root /path/to/another/folder/;
    try_files /maintenance.html =503;
}

What I want to accomplish: normally I pass all requests to my reverse proxy. When proxy is for some reason down, so it throws 502, I want nginx to try to serve files from my local folder (with status 200), and if they're not found either, I want to return my maintenance.html file with status 503.

Unfortunately, this does not work. It just serves me the default nginx 503 page. Why is it so and how do I make it work?

What I'm doing right now is I do try_files $uri @maintenance and then in @maintenance location I do return 503 "<literally the whole HTML page here>", but this is amazingly stupid. How do I serve a static file with a specific status code without hardcoding it into the conf file?

Richard Smith avatar
jp flag
The main problem is that Nginx does not allow `error_page` to be nested. You could use `try_files $uri @maintenance;` in @proxy_down, if you can accept the maintenance page being returned with a 200 status code.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.