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

I am trying to set up an automatic site-down page for nginx. So far I have this:

    location / {
             try_files /sitedown.html @myapp;
    }

    location @myapp {
             ...
    }

That works well enough: if sitedown.html is present, it serves that, otherwise it serves the app. What I'd like to do, however, is respond differently to Ajax requests so that they don't error out the javascript. I believe, using the rewrite module, that I can do something like if ($http_x_requested_with = XMLHttpRequest) { but it's unclear to me how to use this in order to do what I want.

I'd like requests that come with that header to return a simple JSON response like "sitedown" with the appropriate json encoding header. Barring that, it would be nice to return a 503 response code that the javascript could react to.

share|improve this question
add comment

1 Answer

If your client application is well-behaved, it will set the Accept: header in AJAX requests so that the server knows that a JSON response is expected. You might use something like this:

location / {
    set $downfile sitedown.html;
    if ($http_accept ~* application/json) {
        set $downfile sitedown.js;
    }
    try_files $downfile @myapp;
}
share|improve this answer
1  
Unfortunately, that returns an error: "try_files" directive is not allowed here in /etc/nginx... –  dave mankoff Nov 3 '12 at 16:57
    
I edited my answer. Does this work better? Sorry, but I couldn't test it on the spot. –  pino42 Nov 3 '12 at 18:54
add comment

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.