# Rewrite server responses

**URL:** <https://www.ruby-forum.com/t/rewrite-server-responses/195899>\
**Category:** NGINX\
**Created:** [October 2, 2010, 11:51am UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899 "2010-10-02T11:51:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 2, 2010, 11:51am UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/1 "2010-10-02T11:51:08Z")

</div>

Hello list,

I have a nginx server that runs on 127.0.0.1:5004 (behind a load  
balancer) and I’m having difficulties with HTTP responses.  
It seems that the application (in PHP5, Dotclear) sends a 302 when I try  
to connect to the admin panel, and this 302 redirects to :5004.

I there any way to rewrite that response in nginx ?

Thanks,  
Julien

---

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 2, 2010, 11:56am UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/2 "2010-10-02T11:56:24Z")

</div>

On Sat, Oct 02, 2010 at 11:49:50AM +0200, Julien Vehent wrote:

> Hello list,
> 
> I have a nginx server that runs on 127.0.0.1:5004 (behind a load balancer) and I’m having difficulties with HTTP responses.  
> It seems that the application (in PHP5, Dotclear) sends a 302 when I try to connect to the admin panel, and this 302 redirects to :5004.
> 
> I there any way to rewrite that response in nginx ?

port\_in\_redirect off;

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 2, 2010, 12:19pm UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/3 "2010-10-02T12:19:14Z")

</div>

On Sat, 2 Oct 2010 13:55:34 +0400, Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sat, Oct 02, 2010 at 11:49:50AM +0200, Julien Vehent wrote:
> 
> > Hello list,
> > 
> > I have a nginx server that runs on 127.0.0.1:5004 (behind a load balancer) and I’m having difficulties with HTTP responses.  
> > It seems that the application (in PHP5, Dotclear) sends a 302 when I try to connect to the admin panel, and this 302 redirects to :5004.
> > 
> > I there any way to rewrite that response in nginx ?
> 
> port\_in\_redirect off;

I just put that in the location section, put it doesn’t seem to take  
effect.

* * *

GET /blog/admin/ HTTP/1.1  
Host: [example.net](http://example.net)  
[snip]

## HTTP/1.1 302 Moved Temporarily Server: nginx/0.7.67 Date: Sat, 02 Oct 2010 10:14:30 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive Expires: Mon, 26 Jul 1997 05:00:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Location: [http://example.net:5004/blog/admin/auth.php](http://example.net:5004/blog/admin/auth.php)

The conf:

* * *

server {  
listen 127.0.0.1:5004;  
server\_name [example.net](http://example.net);

```
    access_log /var/log/nginx/example.net.access.log;
    root /var/www;
    index index.php index.html index.htm;
    location /ressources {
            autoindex on;
    }
    location /blog/admin {
            port_in_redirect off;
    }
    location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass unix:/var/run/spawn-fcgi-php5.socket;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME 

```

## $document\_root$fastcgi\_script\_name; } }

Julien

---

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 4, 2010, 6:22pm UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/4 "2010-10-04T18:22:01Z")

</div>

Try putting it into the php location instead.

Posted at Nginx Forum:

> **[Re: rewrite server responses](https://forum.nginx.org/read.php?2,136235,136986#msg-136986)**
>
> Hello list, I have a nginx server that runs on 127.0.0.1:5004 (behind a load balancer) and I'm having difficulties with HTTP responses. It seems that the application (in PHP5, Dotclear) sends a 302 when I try to connect to the admin panel, and this...

---

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 5, 2010, 12:56pm UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/5 "2010-10-05T12:56:19Z")

</div>

Hello!

On Mon, Oct 04, 2010 at 09:36:24PM +0200, Julien Vehent wrote:

> ```
> fastcgi_pass unix:/var/run/spawn-fcgi-php5.socket;
> fastcgi_index index.php;
> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
> }
> 
> ```
> 
> * * *
> 
> Could it be cached somewhere ?  
> I have haproxy as a frontend, but it really only does HTTP requests/responses balancing.

As long as you use fastcgi and your fastcgi application  
returns wrong redirect - you should convince it to return correct  
one by providing appropriate fastcgi\_param’s.

Changing fastcgi\_param SERVER\_PORT to 80 should be enough as far  
as I understand your case.

Rewriting of ‘Location’ header returned by backend only available  
for proxy (see proxy\_redirect directive), where it’s not possible  
to control backend’s idea about correct server address.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 4, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/6 "2010-10-04T21:37:32Z")

</div>

On Mon, 04 Oct 2010 12:20:54 -0400, “Dayo” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Try putting it into the php location instead.

Still the same…

* * *

```
    location ~ \.php$ {
            port_in_redirect off;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/spawn-fcgi-php5.socket;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME 

```

## $document\_root$fastcgi\_script\_name; }

Could it be cached somewhere ?  
I have haproxy as a frontend, but it really only does HTTP  
requests/responses balancing.

Julien

---

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 5, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/7 "2010-10-05T13:39:04Z")

</div>

On Tue, 5 Oct 2010 14:55:22 +0400, Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> for proxy (see proxy\_redirect directive), where it’s not possible  
> to control backend’s idea about correct server address.
> 
> Maxim D.

Got it ! Indeed, it was a FASTCGI environment variable. but not that  
one.

I had

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi\_param REDIRECT\_STATUS 200;

And by commenting it out, I don’t have this redirect problem anymore.

That makes me think: where can I find detailled information about  
FASTCGI environment variables and their meanings ? Some, like  
SERVER\_PORT are easy, but what about REMOTE\_PORT for example ?

Thanks,  
Julien

---

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 5, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/8 "2010-10-05T13:44:55Z")

</div>

On Tue, 05 Oct 2010 13:37:52 +0200, Julien Vehent  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Rewriting of ‘Location’ header returned by backend only available
> 
> And by commenting it out, I don’t have this redirect problem anymore.

Correction, setting SERVER\_PORT to 80 was necessary as well.

Julien

---

<div class="post-metadata">

**Author:** ![Julien\_Vehent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@Julien\_Vehent](https://www.ruby-forum.com/u/Julien_Vehent)\
**Post date:** [October 5, 2010, 2:00pm UTC](https://www.ruby-forum.com/t/rewrite-server-responses/195899/9 "2010-10-05T14:00:08Z")

</div>

On Tue, Oct 05, 2010 at 01:37:52PM +0200, Julien Vehent wrote:

> That makes me think: where can I find detailled information about FASTCGI environment variables and their meanings ? Some, like SERVER\_PORT are easy, but what about REMOTE\_PORT for example ?

[http://www.rfc-editor.org/rfc/rfc3875.txt](http://www.rfc-editor.org/rfc/rfc3875.txt) may be a good start. It’s  
about CGI but quite a lot of it applies to FastCGI as well.

REMOTE\_PORT is the client’s TCP port. The connection between the client  
and the web server is $REMOTE\_ADDR:$REMOTE\_PORT →   
$SERVER\_ADDR:$SERVER\_PORT

Best regards,  
Grzegorz N.
