# How to proxy\_pass POST with headers

**URL:** <https://www.ruby-forum.com/t/how-to-proxy-pass-post-with-headers/216170>\
**Category:** NGINX\
**Created:** [January 27, 2012, 3:03pm UTC](https://www.ruby-forum.com/t/how-to-proxy-pass-post-with-headers/216170 "2012-01-27T15:03:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 27, 2012, 3:03pm UTC](https://www.ruby-forum.com/t/how-to-proxy-pass-post-with-headers/216170/1 "2012-01-27T15:03:29Z")

</div>

Hi,

I’m using Typo3 as CMS with Apache on Ubuntu 10.04. I’m trying to set  
Nginx as a reverse proxy with a typo3 extension evo\_nginx\_boost that  
uses memcached. I have everything running on the same machine, Nginx on  
port 80 and apache behind a firewall on port 8085 and memcached behind a  
on port 11211.

So far I have got everything else working just fine except sending any  
POST information, ex. logging in, etc. This causes browser to try to  
fetch images, etc. behind firewall (port 8085) and also Typo3 doesn’t  
know how to parse the url to right place (it’s creating nice urls.  
Instead of foo.bar/index.php?id=123 it uses  
foo.bar/this/is/the/file.html)

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!

Here is my Nginx conf for the server:

server {  
listen 80;  
server\_name foo.bar;

```
 access_log /var/log/nginx/localhost.access.log;
 error_log /var/log/nginx/error.log debug;

 location / {

    set $domain "http://foo.bar";

 if ($request_method = POST)
    {
        proxy_pass http://foo.bar:8085;
        break;
    }

# set default memcache key
       set $href $host$request_uri;

  if ($http_cookie ~* "be_typo_user")
    {
       proxy_pass http://foo.bar:8085;
       break;
    }

  if ($http_cookie ~* "nginx_boost_fe_user=([^;]+)(?:;|$)")
    {
       set $href $host$request_uri$1;
    }

       set $memcached_key $href;

# Check if local memcached server can answer this request
       default_type text/html;
       memcached_pass 127.0.0.1:11211;

# Send to app. server if Memcached could not answer the request
      error_page 404 502 = /cache_miss$request_uri;

   }
# Main location
  location /cache_miss/
    {
      proxy_pass http://foo.bar:8085;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

location ~ 

```

^/(images|javascript|js|css|flash|media|fileadmin|typo3temp|static|uploads)/  
{  
proxy\_pass [http://foo.bar:8085](http://foo.bar:8085);  
proxy\_set\_header Host $http\_host;  
proxy\_set\_header X-Forwarded-Host $http\_host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
}  
}

server {  
listen 80 default\_server;  
server\_name \_;  
return 301 http://foo.bar$request\_uri;  
}

* * *

Jaakko P.

---

<div class="post-metadata">

**Author:** ![Jaakko\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@Jaakko\_P](https://www.ruby-forum.com/u/Jaakko_P)\
**Post date:** [January 27, 2012, 3:13pm UTC](https://www.ruby-forum.com/t/how-to-proxy-pass-post-with-headers/216170/2 "2012-01-27T15:13:23Z")

</div>

On 27 Jan 2012 14h02 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> try to fetch images, etc. behind firewall (port 8085) and also Typo3  
> doesn’t know how to parse the url to right place (it’s creating nice  
> urls. Instead of foo.bar/index.php?id=123 it uses  
> foo.bar/this/is/the/file.html)
> 
> 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!

For starters: [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/IfIsEvil)

— appa

---

<div class="post-metadata">

**Author:** ![Jaakko\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@Jaakko\_P](https://www.ruby-forum.com/u/Jaakko_P)\
**Post date:** [January 27, 2012, 10:25pm UTC](https://www.ruby-forum.com/t/how-to-proxy-pass-post-with-headers/216170/3 "2012-01-27T22:25:04Z")

</div>

Antnio P. P. Almeida kirjoitti 27.1.2012 kello 16.12:

> For starters: [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/IfIsEvil)

This actually solved my problem!

I changed the if statement to:

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

and added:

error\_page 418 = @post;

location @post {  
proxy\_pass [http://foo.bar:8085](http://foo.bar:8085);  
proxy\_set\_header Host $http\_host;  
proxy\_set\_header X-Forwarded-Host $http\_host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
}

Now it works flawlessly!

Thanks!

* * *

Jaakko P.

---

<div class="post-metadata">

**Author:** ![Jaakko\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@Jaakko\_P](https://www.ruby-forum.com/u/Jaakko_P)\
**Post date:** [January 27, 2012, 10:28pm UTC](https://www.ruby-forum.com/t/how-to-proxy-pass-post-with-headers/216170/4 "2012-01-27T22:28:10Z")

</div>

27 января 2012, 18:03 от Jaakko P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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](http://foo.bar:8085/$1);  
if ($args) {  
set $proxy\_url [http://foo.bar:8085/$1?$args](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

---

<div class="post-metadata">

**Author:** ![Jaakko\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@Jaakko\_P](https://www.ruby-forum.com/u/Jaakko_P)\
**Post date:** [January 28, 2012, 10:59am UTC](https://www.ruby-forum.com/t/how-to-proxy-pass-post-with-headers/216170/5 "2012-01-28T10:59:59Z")

</div>

On Saturday 28 January 2012 01:27:29 Max wrote:  
[…]

> location ~ ^/post\_redirect/(.\*)$ {  
> internal;  
> proxy\_set\_header Host $http\_host;  
> set $proxy\_url [http://foo.bar:8085/$1](http://foo.bar:8085/$1);  
> if ($args) {  
> set $proxy\_url [http://foo.bar:8085/$1?$args](http://foo.bar:8085/$1?$args);  
> }  
> proxy\_pass $proxy\_url;  
> }  
> […]

proxy\_pass [http://foo.bar:8085/$1$is\_args$args](http://foo.bar:8085/$1$is_args$args);

but, actually, this would be better:

location /post\_redirect/ {  
internal;  
proxy\_pass [http://foo.bar:8085/](http://foo.bar:8085/);  
proxy\_redirect off;  
}

wbr, Valentin V. Bartenev
