Is it possible/how can I configure an Nginx location block to proxy to different backends depending on the request method (ie. GET/POST)?
The reason is, I am currently handling the 2 methods at 2 different URLs (one via http proxy and the other via fcgi) and am trying to make it more "REST"ful so, would ideally like the GETting the resource to return the list, while POSTing to the same resource should add to the list.
I don't use this configuration, but based on the examples here:
If your writing your own application, you can also consider checking GET/POST in it, and sending X-Accel-Redirect headers to hand off transport of the files to nginx.
Although you could achieve this with
if
, this is generally discouraged by the Nginx documentation, becauseif
doesn't play well with other directives. For example, assume that GET should be open for everyone, while POST is only for authenticated users, using HTTP Basic Auth. That would requireif
to be combined withauth_basic
, which doesn't work properly.Here is an alternative that works without
if
. The trick is to use "GET" and "POST" as part of the upstream names, so these can be addressed by variable substitution:To combine this with HTTP Basic Auth for everything but GET, just add a
limit_except
block:I couldn't get the answer from @timmmmmy to work, but it pointed me to the map documentation and this worked for me:
This is what i did to make things working for me
Slight change to vog's answer to include a default handler for other methods like OPTIONS, PUT, etc.
@timmmmmy 's answer with the fixed syntax errors (without edit because other person refers to the original answer)