# Nginx and conditional proxy\_next\_upstream directive

**URL:** <https://www.ruby-forum.com/t/nginx-and-conditional-proxy-next-upstream-directive/244050>\
**Category:** NGINX\
**Created:** [February 11, 2016, 4:15pm UTC](https://www.ruby-forum.com/t/nginx-and-conditional-proxy-next-upstream-directive/244050 "2016-02-11T16:15:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [February 11, 2016, 4:15pm UTC](https://www.ruby-forum.com/t/nginx-and-conditional-proxy-next-upstream-directive/244050/1 "2016-02-11T16:15:00Z")

</div>

I want nginx to prevent trying the next upstream if the request is a  
POST  
request and the request just timed out. POSTs should only be repeated on  
error. I tried this config to implement it:

if ($request\_method = POST) {  
proxy\_next\_upstream error;  
}

But this fails with:

nginx: [emerg] “proxy\_next\_upstream” directive is not allowed here…

I tried it within location context as well as server context, but got  
always  
the same error while configurations like these are running fine:

if ($request\_method = POST) {  
return 403;  
}

Or just (without if):

proxy\_next\_upstream error;

Is this a bug in nginx or is this related to “if is evil”?  
([If is Evil… when used in location context | NGINX](https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/))

What is the best way to implement the above functionality with ningx?

Posted at Nginx Forum:

> **[Nginx and conditional proxy\_next\_upstream directive](https://forum.nginx.org/read.php?2,264486,264486#msg-264486)**
>
> I want nginx to prevent trying the next upstream if the request is a POST request and the request just timed out. POSTs should only be repeated on error. I tried this config to implement it: if ($request\_method = POST) { proxy\_next\_upstream error; }...

---

<div class="post-metadata">

**Author:** ![dontknowwhoiam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@dontknowwhoiam](https://www.ruby-forum.com/u/dontknowwhoiam)\
**Post date:** [February 23, 2016, 4:09pm UTC](https://www.ruby-forum.com/t/nginx-and-conditional-proxy-next-upstream-directive/244050/2 "2016-02-23T16:09:57Z")

</div>

Is this question to stupid or has nobody an answer on it? 😉

I just think a POSTs are not idempotent and should never be repeated for  
a  
technical reason. Is there any change to configure nginx in a way to try  
the  
next upstream only if the first one really failed when using POST  
requests?  
Timed out requests on one upstream could potentially still be processed  
by  
an application. Handing this request over to the next upstream must not  
cause the same result as the first upstream since POSTs are not  
idempotent.

Any help would be appreciated.

Posted at Nginx Forum:

> **[Re: Nginx and conditional proxy\_next\_upstream directive](https://forum.nginx.org/read.php?2,264486,264734#msg-264734)**
>
> I want nginx to prevent trying the next upstream if the request is a POST request and the request just timed out. POSTs should only be repeated on error. I tried this config to implement it: if ($request\_method = POST) { proxy\_next\_upstream error; }...

---

<div class="post-metadata">

**Author:** ![dontknowwhoiam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@dontknowwhoiam](https://www.ruby-forum.com/u/dontknowwhoiam)\
**Post date:** [February 24, 2016, 2:42am UTC](https://www.ruby-forum.com/t/nginx-and-conditional-proxy-next-upstream-directive/244050/3 "2016-02-24T02:42:00Z")

</div>

FYI, possible related issue on nginx-dev mail list:

> **[\[PATCH\] Upstream: Add "before\_send" option to the 'proxy\_next\_upstream'.](https://forum.nginx.org/read.php?29,264637,264637#msg-264637)**
>
> \# HG changeset patch # User Yusuke Nojima \<nojima@ynojima.com\> # Date 1455896920 0 # Fri Feb 19 15:48:40 2016 +0000 # Node ID 88bbee2e18f6d475ea7ed8ab61b0cb8142c535dc # Parent 5dfc63c1d9ca5ff5a21a2b0238a2b2782549f62c Upstream: Add...
