I want to configure the server to show a maintenance page when it exist. I tried this code and works:
location / {
try_files /maintenance.html $uri $uri/ @codeigniter;
}
But I noticed it would be served with a 200 status code, and it can cause confusion to search engines. I think the best practice would be returning a 503 status code. On google I find several relevant pages about it, like this. However, they use if to make the redirect and according to nginx documentation it isn't safe to use ifs.
Is there a way of doing it without using if? Is safe to use if in this case?
Thanks.
Here is what I do.
If the file is there it will show the maintenance page. Once you remove the file you will go back to normal.
I think you mean 503 instead of 500.
No. Only
return
is 100% safe insideif
inlocation
context.According to nginx documentation, you can specify an HTTP status code as the last argument to
try_files
. I've tried this but it didn't work.Yes, it's important to use HTTP 503 for temp. redirects. That's the way I've solved it:
I've also written a blog post on that topic:
https://www.joergfelser.at/redirecting-to-a-custom-nginx-maintenance-page/
Happy maintenancing ;)
If the maintenance page only has HTML
No CSS, JS, ...
Without using
if
For maintenance page that has CSS / JS / ...
The flow of this is:
/
, return status 503root
, return content with status 200/
You can put the config in a
maintenance.conf
,include maintenance.conf
when in maintenance.if (-f <file>)
because it will try to check if the file exist on every request