# Port\_in\_redirect not working?

**URL:** <https://www.ruby-forum.com/t/port-in-redirect-not-working/213625>\
**Category:** NGINX\
**Created:** [November 7, 2011, 5:18pm UTC](https://www.ruby-forum.com/t/port-in-redirect-not-working/213625 "2011-11-07T17:18:00Z")\
**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:** [November 7, 2011, 5:18pm UTC](https://www.ruby-forum.com/t/port-in-redirect-not-working/213625/1 "2011-11-07T17:18:00Z")

</div>

Hi all,

I posted this question on SO but haven’t received much attention, so I  
figured here would be better.

Basically I am attempting to run varnish on port 80 and my nginx server  
on 8080, however any request to 80 redirects and appends the port to the  
URL.

i.e. [http://site.com/](http://site.com/) → [http://site.com:8080/](http://site.com:8080/)

From everything I’ve read it seems that ‘port\_in\_redirect off’ should  
disable this, however it does not appear to be working. I’ve used a  
workaround for php by using “fastcgi\_param SERVER\_PORT 80;”, which  
works, but requests to my “location /” are getting the port appended.

My server setup is:

server {  
listen 8080 default;  
server\_name “” xxx.xxx.xxx.xxx; #just using IP here (no domain  
yet)

```
port_in_redirect off;
server_name_in_redirect off;

access_log /var/log/nginx/localhost.access.log;

location / {
        root /var/www/site/html/;
        index index index.php;
        try_files $uri/ $uri /index.php?q=$uri&$args;
}

```

}  
upstream backend {  
server 127.0.0.1:9000;  
}

I have tried port\_in\_redirect in both the “location /” and the server  
blocks, but neither works.

Can anyone shed any light on this problem? I’m currently using nginx  
v1.0.9 on a ubuntu server.

[Link to SO question (full .conf there):  
[varnish - Nginx port\_in\_redirect not working? - Stack Overflow](http://stackoverflow.com/questions/8026763/nginx-port-in-redirect-not-working)]

Posted at Nginx Forum:

> **[port\_in\_redirect not working?](https://forum.nginx.org/read.php?2,217928,217928#msg-217928)**
>
> Hi all, I posted this question on SO but haven't received much attention, so I figured here would be better. Basically I am attempting to run varnish on port 80 and my nginx server on 8080, however any request to 80 redirects and appends the port to...

---

<div class="post-metadata">

**Author:** ![olan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/59ef9b/32.png) [@olan](https://www.ruby-forum.com/u/olan)\
**Post date:** [November 7, 2011, 6:27pm UTC](https://www.ruby-forum.com/t/port-in-redirect-not-working/213625/2 "2011-11-07T18:27:16Z")

</div>

Hello!

On Mon, Nov 07, 2011 at 11:17:28AM -0500, olan wrote:

> yet)  
> try\_files $uri/ $uri /index.php?q=$uri&$args;  
> v1.0.9 on a ubuntu server.
> 
> [Link to SO question (full .conf there):  
> [http://stackoverflow.com/questions/8026763/nginx-port-in-redirect-not-working](http://stackoverflow.com/questions/8026763/nginx-port-in-redirect-not-working)]

The “port\_in\_redirect” directive only alters behaviour of nginx  
itself (for trailing slash redirects, rewrites and so on), and  
it’s expected that it does nothing for redirects returned by php.

With “fastcgi\_param SERVER\_PORT 80;” php should be fine, and I  
suspect that problem you see is actually related to your browser  
(or varnish) cache sitting here from previous configurations.

If clearing cache doesn’t help, please provide debug log (see  
[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/Debugging)), it will show what goes on here.

Maxim D.

---

<div class="post-metadata">

**Author:** ![olan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/59ef9b/32.png) [@olan](https://www.ruby-forum.com/u/olan)\
**Post date:** [November 7, 2011, 7:26pm UTC](https://www.ruby-forum.com/t/port-in-redirect-not-working/213625/3 "2011-11-07T19:26:39Z")

</div>

Hi Maxim, thanks for getting back to me.

I’ve enabled debugging and the output is here:  
[Nginx Debug - Pastebin.com](http://pastebin.com/Dmw2nJMY). I’ve cleared all caches and restarted  
varnish, nginx and php5-fpm and mysql.

I didn’t explain myself very well earlier. The “fastcgi\_param  
SERVER\_PORT 80” does work for php pages (it’s in my location ~ .php$  
block) but the problem I’m having is with my server root.

[http://site.com/page.php](http://site.com/page.php) ← works fine  
[http://site.com/](http://site.com/) redirects to [http://site.com:8080](http://site.com:8080)  
[http://site.com/index.php](http://site.com/index.php) redirects to [http://site.com:8080](http://site.com:8080)

One thing I noticed is “location /” block is used first, the index.php  
is tried, and then the “location ~ .php$” block processes this page. I  
would have thought that the SERVER\_PORT would have rewritten the port  
used at this stage…?

Any help is greatly appreciated!  
Thanks,

Olan

Posted at Nginx Forum:

> **[Re: port\_in\_redirect not working?](https://forum.nginx.org/read.php?2,217928,217939#msg-217939)**
>
> Hi all, I posted this question on SO but haven't received much attention, so I figured here would be better. Basically I am attempting to run varnish on port 80 and my nginx server on 8080, however any request to 80 redirects and appends the port to...

---

<div class="post-metadata">

**Author:** ![olan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/59ef9b/32.png) [@olan](https://www.ruby-forum.com/u/olan)\
**Post date:** [November 7, 2011, 8:46pm UTC](https://www.ruby-forum.com/t/port-in-redirect-not-working/213625/4 "2011-11-07T20:46:36Z")

</div>

On 7 November 2011 22:44, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can try tricking php by putting $\_SERVER[“SERVER\_PORT”] right at  
> the top of your index php (or use autoprepend in php.ini so it applies  
> to all php files).

That should read - You can try tricking php by putting  
$\_SERVER[“SERVER\_PORT”] = “80”;

---

<div class="post-metadata">

**Author:** ![olan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/59ef9b/32.png) [@olan](https://www.ruby-forum.com/u/olan)\
**Post date:** [November 7, 2011, 8:45pm UTC](https://www.ruby-forum.com/t/port-in-redirect-not-working/213625/5 "2011-11-07T20:45:22Z")

</div>

On 7 November 2011 21:25, olan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Maxim, thanks for getting back to me.
> 
> I’ve enabled debugging and the output is here:  
> [Nginx Debug - Pastebin.com](http://pastebin.com/Dmw2nJMY). I’ve cleared all caches and restarted  
> varnish, nginx and php5-fpm and mysql.

Line 245 of your debug shows the issue. php is sending a header - http  
fastcgi header: “Location: [http://XXX.XXX.XXX.XXX:8080/](http://XXX.XXX.XXX.XXX:8080/)”. The  
proxy\_redirect directive handles this for proxy\_pass setups but there  
isn’t an equivalent for fastcgi.

You can try tricking php by putting $\_SERVER[“SERVER\_PORT”] right at  
the top of your index php (or use autoprepend in php.ini so it applies  
to all php files).

I just had a problem with this with a proxy setup as the  
proxy\_redirect only accepts variables in the redirect part such that I  
had to do …

proxy\_redirect [http://example-1.com:8080/](http://example-1.com:8080/)[http://example-1.com/](http://example-1.com/);  
proxy\_redirect [http://example-2.com:8080/](http://example-2.com:8080/)[http://example-2.com/](http://example-2.com/);  
proxy\_redirect [http://example-3.com:8080/](http://example-3.com:8080/)[http://example-3.com/](http://example-3.com/);  
…  
proxy\_redirect [http://example-n.com:8080/](http://example-n.com:8080/)[http://example-n.com/](http://example-n.com/);

If the directive accepted variables in the original uri part, a single  
line …

proxy\_redirect http://:8080$host/http://$host/;

… would have done the job and given flexibility. I have to remember  
to add every domain I create to the list.

Anyway, I digress. Try the trick on PHP. Nothing can be done in Nginx  
until a fastcgi\_redirect directive is introduced.
