How to proxy_pass POST with headers

Max nginxyz at mail.ru
Fri Jan 27 21:27:29 UTC 2012


27 января 2012, 18:03 от Jaakko Pasanen <jkpasanen at gmail.com>:

> I think that In order to get it work I would somehow need to be able to set
> proxy_set_header Host $http_host;, but Nginx doesn't let me do that in
> if-statement (see below). I really don't know how to resolve this and googling
> around didn't help me, so I would be extremely happy if anybody could give me
> any feedback! Thanks!

This should work:

location / {
  if ($request_method = POST) {
   rewrite ^/(.*)$ /post_redirect/$1 last;
  }
}

location ~ ^/post_redirect/(.*)$ {
  internal;
  proxy_set_header Host $http_host;
  set $proxy_url http://foo.bar:8085/$1;
  if ($args) {
    set $proxy_url http://foo.bar:8085/$1?$args;
  }
  proxy_pass $proxy_url;
}

Since rewrite is guaranteed to work without side-effects
inside if blocks, the safest approach would be to first
use rewrite inside the if block (with last) to change the
URI to an internal URI and then handle the rest inside
the internal URI location block.

Max


More information about the nginx mailing list