Wanted: suggestions on how to invert proxy_pass return codes

Jonathan Matthews contact at jpluscplusm.com
Mon Mar 4 23:58:07 UTC 2013

Hi all -

I have an odd requirement which I'm not quite able to solve - is there
anyone out there with a solution?

For reasons I won't go into, but which are unfortunately immovable and
quite real, I need to come up with a location stanza that exhibits the
following behaviour:

location /healthcheck/ {
  if (!-f /tmp/flag) {
    return 503;
  proxy_intercept_errors on;
  proxy_pass http://backend.server.fqdn.local/status;
  error_page 200 = @backend_up;
  error_page 404 500 501 502 503 504 = @backend_down;
location @backend_up {
  return 503;
location backend_down {
  return 200;

Yes - you read that correctly :-(

Before making the proxy_pass call, check a marker on disk and serve a
(real, not translated to =200) 503 if it exists.
If I get a 200 from the backend status page, actually serve a 503.
If I get a 404/5xx from the backend status page, serve a 200.

I'm pretty sure the last time I tried something horrible like this,
error_page complained that it could only override response codes >400
- hence this email. Can anyone see a way of achieving the behaviour I

The behaviour described is *all* that's important - no other part of
the config, above, is fixed. I can't introduce any more external
services unless there's absolutely no choice, however.

Thanks for having a think about this. I'll tell you what it's for if
you're able to help - I'm pretty sure you'll be both surprised and a
little bit proud that your idea got into this project, if you do ...

TIA all,
Jonathan Matthews // Oxford, London, UK

More information about the nginx mailing list