Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to setup nginx to return a 503 if a particular file exists (probably something like "upgrading"). I'm trying to use the try_files directive, but when it finds the /upgrading.html file, it serves it rather than following the directive. Why is that?

location / {
    try_files /upgrading.html @keepgoing;
}

location = /upgrading.html {
    return 503;
}

location @keepgoing {
    #do stuff here to do whatever I would normally do...
}

In the log when I turn on debugging I see the following:

3388    2010/07/01 19:44:21 [debug] 76327#0: *8 test location: "/"
3389    2010/07/01 19:44:21 [debug] 76327#0: *8 using configuration "/"
3390    2010/07/01 19:44:21 [debug] 76327#0: *8 http cl:-1 max:52428800
3391    2010/07/01 19:44:21 [debug] 76327#0: *8 generic phase: 2
3392    2010/07/01 19:44:21 [debug] 76327#0: *8 post rewrite phase: 3
3393    2010/07/01 19:44:21 [debug] 76327#0: *8 generic phase: 4
3394    2010/07/01 19:44:21 [debug] 76327#0: *8 generic phase: 5
3395    2010/07/01 19:44:21 [debug] 76327#0: *8 access phase: 6
3396    2010/07/01 19:44:21 [debug] 76327#0: *8 access phase: 7
3397    2010/07/01 19:44:21 [debug] 76327#0: *8 post access phase: 8
3398    2010/07/01 19:44:21 [debug] 76327#0: *8 try files phase: 9
3399    2010/07/01 19:44:21 [debug] 76327#0: *8 try to use file: "/upgrading.html" "/usr/local/nginx/html/upgrading.html"
3400    2010/07/01 19:44:21 [debug] 76327#0: *8 try file uri: "/upgrading.html"
3401    2010/07/01 19:44:21 [debug] 76327#0: *8 content phase: 10
3402    2010/07/01 19:44:21 [debug] 76327#0: *8 content phase: 11
3403    2010/07/01 19:44:21 [debug] 76327#0: *8 content phase: 12
3404    2010/07/01 19:44:21 [debug] 76327#0: *8 http filename: "/usr/local/nginx/html/upgrading.html"

Seems like it can't find the directive, but it's there, so not sure what I'm doing wrong.

Also, more generally, is this an acceptable approach to solving this problem? Other ways to do it?

share|improve this question

1 Answer 1

From this page: https://calomel.org/nginx.html

## System Maintenance (Service Unavailable) 
if (-f $document_root/system_maintenance.html ) {
    error_page 503 /system_maintenance.html;
    return 503;
}

The important part being the '-f', I believe - it tests to see if the file exists.

share|improve this answer
    
Not recommended: wiki.nginx.org/IfIsEvil try_files is the correct approach. –  Martin Fjordvald Jul 2 '10 at 3:02
    
quote from there The only safe things may be done inside if in location context are: return ...; and rewrite ... last; –  SaveTheRbtz Jul 2 '10 at 9:13
    
Martin, SaveTheRbtz: the example I pointed to did not use 'if' within a 'location' statement - it was at the 'server' level, which is considered fine. However, it is good to point out that it's not recommended within a 'location' statement - that might come up for the OP or anyone else stumbling across this reply. –  James Addison Jul 2 '10 at 14:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.