0

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.

dave mankoff
  • 121
  • 5

1 Answers1

1

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;
}
pino42
  • 915
  • 5
  • 11