# Nginx redirecting to wrong port

**URL:** <https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873>\
**Category:** NGINX\
**Created:** [April 4, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873 "2008-04-04T15:42:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [April 4, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/1 "2008-04-04T15:42:35Z")

</div>

hi,

nginx runs behind a big-ip loadbalancer. the load-balancer accepts  
connections on port 80, but connects to nginx on port 8080.  
whenever i request a directory without trailing slash, nginx redirects  
to the directory with slash, but on the wrong port.

[http://www.mysite.com/directory](http://www.mysite.com/directory) → [http://www.mysite.com:8080/directory](http://www.mysite.com:8080/directory).

any idea how to tell nginx, that the port is 80?

thanks

## jodok batlogg

“Beautiful is better than ugly.”  
– The Zen of Python, by Tim Peters

Jodok B., Lovely Systems GmbH  
Schmelzhütterstraße 26a, 6850 Dornbirn, Austria  
mobile: +43 664 9636963, phone: +43 5572 908060

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [April 4, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/2 "2008-04-04T18:12:23Z")

</div>

On 04.04.2008, at 15:39, Roxis wrote:

> > any idea how to tell nginx, that the port is 80?
> 
> [http://wiki.codemongers.com/NginxHttpCoreModule#port\_in\_redirect](http://wiki.codemongers.com/NginxHttpCoreModule#port_in_redirect)
> 
> syntax: port\_in\_redirect [on|off]  
> default: port\_in\_redirect on  
> context: http, server, location
> 
> Directive allows or prevents port indication in redirects handled by  
> nginx.

thanks i misinterpreted the docs before 🙂

jodok

> 

–  
“Beautiful is better than ugly.”  
– The Zen of Python, by Tim Peters

Jodok B., Lovely Systems GmbH  
Schmelzhütterstraße 26a, 6850 Dornbirn, Austria  
mobile: +43 664 9636963, phone: +43 5572 908060

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [April 4, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/3 "2008-04-04T15:49:07Z")

</div>

On Friday 04 April 2008, Jodok B. wrote:

> nginx runs behind a big-ip loadbalancer. the load-balancer accepts  
> connections on port 80, but connects to nginx on port 8080.  
> whenever i request a directory without trailing slash, nginx redirects  
> to the directory with slash, but on the wrong port.
> 
> [http://www.mysite.com/directory](http://www.mysite.com/directory) → [http://www.mysite.com:8080/directory](http://www.mysite.com:8080/directory).
> 
> any idea how to tell nginx, that the port is 80?

[http://wiki.codemongers.com/NginxHttpCoreModule#port\_in\_redirect](http://wiki.codemongers.com/NginxHttpCoreModule#port_in_redirect)

syntax: port\_in\_redirect [on|off]  
default: port\_in\_redirect on  
context: http, server, location

Directive allows or prevents port indication in redirects handled by  
nginx.

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [May 27, 2008, 10:08am UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/4 "2008-05-27T10:08:52Z")

</div>

Hello,

I have the same problem. Did you find any solution?

I have 2 servers, one with nginx+apache1.3 and second with  
nginx+apache2.  
The problem is by apache1.3 only, so it must be problem of apache1.3 and  
it should be set up in apache1.3 config file.

I tryed port\_in\_redirect on in nginx.conf, but with no sucsess.

I am sure it does apache1.3, but how to fix it I have no idea?

Thanks for any help:)

Dusan

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [May 27, 2008, 10:42am UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/5 "2008-05-27T10:42:23Z")

</div>

On Tue, May 27, 2008 at 10:08:52AM +0200, Dusan Turko wrote:

> I have the same problem. Did you find any solution?
> 
> I have 2 servers, one with nginx+apache1.3 and second with  
> nginx+apache2.  
> The problem is by apache1.3 only, so it must be problem of apache1.3 and  
> it should be set up in apache1.3 config file.
> 
> I tryed port\_in\_redirect on in nginx.conf, but with no sucsess.
> 
> I am sure it does apache1.3, but how to fix it I have no idea?

Could you show your proxied location ?

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [May 27, 2008, 11:05am UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/6 "2008-05-27T11:05:22Z")

</div>

On Tue, May 27, 2008 at 10:55:23AM +0200, Dusan Turko wrote:

> ```
> proxy_temp_file_write_size 64k;
> }
> 
> ```
> 
> I use the same settings with apache2 on the other machine and there is  
> no problem. As I told, the problem occurs with apache1.3
> 
> If I type in a browser [http://www.mysite.com/test/](http://www.mysite.com/test/) is ok but when i type  
> [http://www.mysite.com/test](http://www.mysite.com/test) then it redirect to  
> [http://www.mysite.com:8080/test/](http://www.mysite.com:8080/test/)

This is because you set  
proxy\_redirect off;  
and Apache 1.3 is configured to use www.mysite.com:8080 in redirects.

You may either fix Apache configuration or set in nginx:

- 

```
     proxy_redirect off;

```

- 

```
     proxy_redirect http://www.mysite.com:8080/ /;

```

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [May 27, 2008, 10:55am UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/7 "2008-05-27T10:55:23Z")

</div>

> Could you show your proxied location ?

```
location / {
            proxy_pass http://localhost:8080/;
            proxy_redirect off;
    port_in_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For 
$proxy_add_x_forwarded_for;

            client_max_body_size 10m;
            client_body_buffer_size 128k;

            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_read_timeout 90;

            proxy_buffer_size 4k;
            proxy_buffers 4 32k;
      proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
    }
```

I use the same settings with apache2 on the other machine and there is  
no problem. As I told, the problem occurs with apache1.3

If I type in a browser [http://www.mysite.com/test/](http://www.mysite.com/test/) is ok but when i type  
[http://www.mysite.com/test](http://www.mysite.com/test) then it redirect to  
[http://www.mysite.com:8080/test/](http://www.mysite.com:8080/test/)

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [May 27, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/8 "2008-05-27T13:29:23Z")

</div>

> You may either fix Apache configuration or set in nginx:
> 
> - 
> 
> ```
> proxy_redirect off;
> 
> ```
> 
> - 
> 
> ```
> proxy_redirect http://www.mysite.com:8080/ /;
> 
> ```

Thanks for your reply

1.)It helped just when I type [http://www.mysite.com/test](http://www.mysite.com/test) it shows a  
blank site (page does not exist) instead of redirect to  
[http://www.mysite.com/test/](http://www.mysite.com/test/)

2.) I use more virtual hosts on the server so I tryed to set up  
proxy\_redirect \*:8080/ /;  
and it does not work.

Any idea how to set it up in apache?

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [May 27, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/9 "2008-05-27T15:14:02Z")

</div>

On Tue, May 27, 2008 at 01:29:23PM +0200, Dusan Turko wrote:

> > You may either fix Apache configuration or set in nginx:
> > 
> > - 
> > 
> > ```
> > proxy_redirect off;
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > proxy_redirect http://www.mysite.com:8080/ /;
> > 
> > ```
> 
> Thanks for your reply
> 
> 1.)It helped just when I type [http://www.mysite.com/test](http://www.mysite.com/test) it shows a  
> blank site (page does not exist) instead of redirect to  
> [http://www.mysite.com/test/](http://www.mysite.com/test/)

Could you create debug log of this request ?

> 2.) I use more virtual hosts on the server so I tryed to set up  
> proxy\_redirect \*:8080/ /;  
> and it does not work.

nginx does not support variables in left part of proxy\_redirect,  
therefore  
you should configure Apache.

> Any idea how to set it up in apache?

Look  
[http://httpd.apache.org/docs/1.3/mod/core.html#usecanonicalname](http://httpd.apache.org/docs/1.3/mod/core.html#usecanonicalname)  
[http://httpd.apache.org/docs/1.3/mod/core.html#port](http://httpd.apache.org/docs/1.3/mod/core.html#port)

Try to set  
UseCanonicalName on

and replace  
Port 8080  
with  
Listen 8080

---

<div class="post-metadata">

**Author:** ![Jodok\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@Jodok\_B](https://www.ruby-forum.com/u/Jodok_B)\
**Post date:** [May 27, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/nginx-redirecting-to-wrong-port/133873/10 "2008-05-27T16:22:40Z")

</div>

> Try to set  
> UseCanonicalName on
> 
> and replace  
> Port 8080  
> with  
> Listen 8080

I just changed Listen 8080 instead of Port 8080 and it wors fine now:)  
I left UseCanonicalName off

Thanks a lot!!!
