# Wanted: suggestions on how to invert proxy\_pass return codes

**URL:** <https://www.ruby-forum.com/t/wanted-suggestions-on-how-to-invert-proxy-pass-return-codes/227125>\
**Category:** NGINX\
**Created:** [March 5, 2013, 12:58am UTC](https://www.ruby-forum.com/t/wanted-suggestions-on-how-to-invert-proxy-pass-return-codes/227125 "2013-03-05T00:58:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/fbc32d/32.png) [@jonathan2000](https://www.ruby-forum.com/u/jonathan2000)\
**Post date:** [March 5, 2013, 12:58am UTC](https://www.ruby-forum.com/t/wanted-suggestions-on-how-to-invert-proxy-pass-return-codes/227125/1 "2013-03-05T00:58:46Z")

</div>

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](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 M. // Oxford, London, UK  
[http://www.jpluscplusm.com/contact.html](http://www.jpluscplusm.com/contact.html)

---

<div class="post-metadata">

**Author:** ![jonathan2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/fbc32d/32.png) [@jonathan2000](https://www.ruby-forum.com/u/jonathan2000)\
**Post date:** [March 5, 2013, 1:18am UTC](https://www.ruby-forum.com/t/wanted-suggestions-on-how-to-invert-proxy-pass-return-codes/227125/2 "2013-03-05T01:18:11Z")

</div>

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 M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 M. // Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**

---

<div class="post-metadata">

**Author:** ![jonathan2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/fbc32d/32.png) [@jonathan2000](https://www.ruby-forum.com/u/jonathan2000)\
**Post date:** [March 6, 2013, 11:18am UTC](https://www.ruby-forum.com/t/wanted-suggestions-on-how-to-invert-proxy-pass-return-codes/227125/3 "2013-03-06T11:18:42Z")

</div>

On Tue, Mar 05, 2013 at 12:17:03AM +0000, Jonathan M. 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](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](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 [Module ngx\_http\_proxy\_module](http://nginx.org/r/proxy_ignore_headers) for the meaning of  
X-Accel-Redirect.)
