Forum: NGINX Wanted: suggestions on how to invert proxy_pass return codes

Posted by Jonathan Matthews (Guest)
on 2013-03-05 00:58
(Received via mailing list)
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
need?

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
--
Jonathan Matthews // Oxford, London, UK
http://www.jpluscplusm.com/contact.html
Posted by Jonathan Matthews (Guest)
on 2013-03-05 01:18
(Received via mailing list)
A slight thinko crept in to my original mail; there's a small
difference (which does remove a minor complexity) as I've marked below
...

On 4 March 2013 23:58, Jonathan Matthews <contact@jpluscplusm.com> 
wrote:
[snip]
> --------------------------------------------------------------------------
> location /healthcheck/ {
>   if (!-f /tmp/flag) {
>     return 503;
>   }

SHOULD BE:

"
  if (-f /tmp/flag) {
    return 200;
  }
"

[snip]
> --------------------------------------------------------------------------
>
> Before making the proxy_pass call, check a marker on disk and serve a
> (real, not translated to =200) 503 if it exists.

SHOULD BE: "... serve a 200 if it exists."

I don't think this changes the meat of the problem - it just removes
one minor niggle.

Any thoughts?
--
Jonathan Matthews // Oxford, London, UK
http://www.jpluscplusm.com/contact.html
Posted by Ruslan Ermilov (Guest)
on 2013-03-06 11:18
(Received via mailing list)
On Tue, Mar 05, 2013 at 12:17:03AM +0000, Jonathan Matthews wrote:
> >   }
> > --------------------------------------------------------------------------
> >
> > Before making the proxy_pass call, check a marker on disk and serve a
> > (real, not translated to =200) 503 if it exists.
>
> SHOULD BE: "... serve a 200 if it exists."
>
> I don't think this changes the meat of the problem - it just removes
> one minor niggle.
>
> Any thoughts?

Well, if you can add headers on a backend ...

: http {
:     map $connection $fail {
:         ~[02468]$ 1;
:     }
:
:     server {
:         server_name backend;
:         listen 8001;
:         add_header X-Accel-Redirect /backend_up;
:
:         if ($fail) { return 503 "real 503\n"; }
:         return 200 "real 200\n";
:     }
:
:     server {
:         listen 8000;
:
:         location = /test {
:             proxy_pass http://127.0.0.1:8001;
:             proxy_intercept_errors on;
:             error_page 503 = @backend_down;
:         }
:
:         location @backend_down {
:             return 200 "wrapped 200\n";
:         }
:
:         location = /backend_up {
:             internal;
:             return 503 "wrapped 503\n";
:         }
:     }
: }

$ repeat 10 curl http://127.0.0.1:8000/test
wrapped 503
wrapped 200
wrapped 503
wrapped 200
wrapped 503
wrapped 200
wrapped 503
wrapped 200
wrapped 503
wrapped 200

(See http://nginx.org/r/proxy_ignore_headers for the meaning of
X-Accel-Redirect.)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.