# Correct way to setup maintenance page in nginx

**URL:** <https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498>\
**Category:** NGINX\
**Created:** [November 3, 2011, 7:31pm UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498 "2011-11-03T19:31:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [November 3, 2011, 7:31pm UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/1 "2011-11-03T19:31:03Z")

</div>

Hi all,

I tried using this method with `try_files`. Didn’t work. Also the HTTP  
status code(503) is not being set.

```
location / {
    try_files /var/www/during_build.html @maintenance;
        proxy_pass http://localhost:82;
}

location @maintenance {
      return 503;
}

```

This method outside of `location` directive

error\_page 503 /var/www/during\_build.html;

## System Maintenance (Service Unavailable)

if (-f /var/www/during\_build.html) {  
return 503;  
}

Is also not working. Nginx just returns 503 without the custom page.

What is the correct way to show system down pages?

-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 3, 2011, 9:18pm UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/2 "2011-11-03T21:18:47Z")

</div>

On Thursday 03 November 2011 22:30:00 Quintin P. wrote:  
[…]

> What is the correct way to show system down pages?

Try this one:

```
error_page 503 /during_build.html;

location / {

    if (-f /var/www/during_build.html) {
        return 503;
    }

}

location = /during_build.html {
    root /var/www/;
    internal;
}

```

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 3, 2011, 10:43pm UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/3 "2011-11-03T22:43:32Z")

</div>

On Friday 04 November 2011 01:02:11 Antnio P. P. Almeida wrote:

> }  
> }
> 
> location @503 {  
> return 503;  
> }
> 
> No need to use the if and the internal is implicit on the try\_files.

First of all, named locations can be on the server level only.

And, how does a request get into @unavailable? Only after 503 has  
occurred.  
But, what will cause it?

I think the main idea was: when we need to do some maintenance, we just  
create  
a specific file (probably with explaining message for users) and Nginx  
starts  
to return 503 on every request.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 3, 2011, 10:02pm UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/4 "2011-11-03T22:02:46Z")

</div>

On 3 Nov 2011 20h18 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> location / {  
> }  
> Why not this?

root /var/www;

location / {

error\_page 503 @unavailable;

location @unavailable {  
try\_files /during\_building.html @503;  
}  
}

location @503 {  
return 503;  
}

No need to use the if and the internal is implicit on the try\_files.

— appa

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 3, 2011, 10:54pm UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/5 "2011-11-03T22:54:38Z")

</div>

On 11/3/11, Valentin V. Bartenev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think the main idea was: when we need to do some maintenance, we just  
> create  
> a specific file (probably with explaining message for users) and Nginx  
> starts  
> to return 503 on every request.

If you are willing to create some file for this you can just as well  
swap  
nginx.conf with special maintenance config, where you can specify a  
single  
server { } block with return 503 or anything you like.

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 4, 2011, 4:15am UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/6 "2011-11-04T04:15:00Z")

</div>

Thanks Valentin. This worked like a charm.

I spend nearly 2-3 hours trying for a solution that evaded “if” and just  
“try\_files”. Even the official wiki supported that.

There are a lot of solutions out in the wild and most for them don’t  
work.  
Thanks again for the help.

-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 3, 2011, 11:07pm UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/7 "2011-11-03T23:07:00Z")

</div>

On Friday 04 November 2011 01:54:02 Alexandr G. wrote:

> If you are willing to create some file for this you can just as well swap  
> nginx.conf with special maintenance config, where you can specify a single  
> server { } block with return 503 or anything you like.

Of course, you have many options to do this with different pros and  
cons.  
And I just help to solve the problem the way that topic starter wants.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 4, 2011, 9:56am UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/8 "2011-11-04T09:56:48Z")

</div>

On second thought don’t you think the file check in the “if” condition  
is  
expensive considering it will be executed on every hit?

-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 4, 2011, 10:48am UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/9 "2011-11-04T10:48:43Z")

</div>

On Friday 04 November 2011 12:55:45 Quintin P. wrote:

> On second thought don’t you think the file check in the “if” condition is  
> expensive considering it will be executed on every hit?

If you don’t deal with high-load or dedicated file storage with big  
access  
latency, then don’t worry. IMHO.

Also, you can try to tune Nginx open file cache:

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#open_file_cache)**
>
> This directive appeared in version 1.11.8.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [November 4, 2011, 10:57am UTC](https://www.ruby-forum.com/t/correct-way-to-setup-maintenance-page-in-nginx/213498/10 "2011-11-04T10:57:59Z")

</div>

Thanks.
