Setting Up nginx Site Down That Responds Differently to Ajax?

Posted by dave mankoff on Server Fault See other posts from Server Fault or by dave mankoff
Published on 2012-11-03T16:34:13Z Indexed on 2012/11/03 17:05 UTC
Read the original article Hit count: 268

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.

© Server Fault or respective owner

Related posts about nginx

Related posts about redirection