NGINX Setting up error_page 503 for different messages (maintenance and heavy load)

Bill Billingson asked:

I currently have an error page set up as follows:

error_page 503 @503;

location @503 {
  error_page 405 = /system/maintenance.html;
  if (-f $request_filename) {
    break;
  }
  rewrite ^(.*)$ /system/maintenance.html break;
}

This correctly serves the maintenance page when the /system/maintenance.html file is present.

I’ve also created a /503.html which contains a message specific to when the site is under heavy load.

What is the best way to serve error_page 503 /503.html when the site is receiving too much traffic, but also be able to serve the maintenance page with a 503 status code?

My answer:


My first thought would just be to use try_files. That’s what it’s meant for.

For example:

error_page 503 @503;

location @503 {
    try_files /system/maintenance.html /503.html;
}

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.