Discussion:
Returning a 503 status code using try files with a maintenance page activated
Zev Blut
2012-05-10 02:52:39 UTC
Permalink
Hello,

I am looking into streamlining our nginx maintenance page check logic.
In the nginx.org documentation there is an example usage for
try_files with a maintenance page.

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

<example>
location / {
try_files /system/maintenance.html
$uri $uri/index.html $uri.html
@mongrel;
}

location @mongrel {
proxy_pass http://mongrel;
}
</example>

This works, but when the maintenance page is activated it returns an
http status code of 200 instead of a 503.

What works for me on our site is something like this:

<example>
location / {
if (-f /system/maintenance.html) {
return 503;
break;
}

try_files /system/maintenance.html
$uri $uri/index.html $uri.html
@mongrel;
}

location @mongrel {
proxy_pass http://mongrel;
}

error_page 503 /system/maintenance.html;

location = /system/maintenance.html {
root /app;
}
</example>


Is it possible to remove the "if return 503" logic and have the
try_files logic from the nginx documentation, but have nginx return a
503 when the maintenance page is found?

I have a number of location directives that all have this "if return
503" logic and would like to reduce this duplication.

Thanks,
Zev
Zev Blut
2012-05-14 09:04:14 UTC
Permalink
Hello,
Post by Zev Blut
Is it possible to remove the "if return 503" logic and have the
try_files logic from the nginx documentation, but have nginx return a
503 when the maintenance page is found?
I have a number of location directives that all have this "if return
503" logic and would like to reduce this duplication.
I have found a work around to my question.

I push the "if return 503" check into the location that defines
the proxy pass.

My working example becomes:

<example>
location / {
try_files $uri $uri/index.html $uri.html @mongrel;
}

location @mongrel {
if (-f /system/maintenance.html) {
return 503;
break;
}
proxy_pass http://mongrel;
}

error_page 503 /system/maintenance.html;

location = /system/maintenance.html {
root /app;
}

</example>


In the end, I do not use the maintenance path in the try_files.
This may not look like much of a change, but my refactoring helps
reduce the checks when I have multiple location directives that
all end with pass to @mongrel.


Zev

Loading...