# Port forwarding problem

**URL:** <https://www.ruby-forum.com/t/port-forwarding-problem/209918>\
**Category:** NGINX\
**Created:** [July 26, 2011, 12:35pm UTC](https://www.ruby-forum.com/t/port-forwarding-problem/209918 "2011-07-26T12:35:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 26, 2011, 12:35pm UTC](https://www.ruby-forum.com/t/port-forwarding-problem/209918/1 "2011-07-26T12:35:56Z")

</div>

Hi

I have two machines. One is server with nginx and the other is NAT.  
On server I’ve got mediawiki on port 2222. I forwared port 55555 on my  
nat to server:2222. I have one big problem. When I try to open  
NAT\_IP:55555 in my browser it redirects me to http://:2222. What’s  
wrong with my config?

Here is my virtualhost config file:  
1 server {  
2 listen 2222;  
3 server\_name MYNAMEl;  
4 server\_name\_in\_redirect off;  
5 port\_in\_redirect off;  
6  
7 root XXXX;  
8  
9 location / {  
10 index index.php;  
11 error\_page 404 = @mediawiki;  
12 }  
13  
14 location @mediawiki {  
15 rewrite ^/([^?]_)(?:?(._))? /index.php?title=$1&$2 last;  
16 }  
…  
44 }

Posted at Nginx Forum:

> **[Port forwarding problem](https://forum.nginx.org/read.php?2,212938,212938#msg-212938)**
>
> Hi I have two machines. One is server with nginx and the other is NAT. On server I've got mediawiki on port 2222. I forwared port 55555 on my nat to server:2222. I have one big problem. When I try to open NAT\_IP:55555 in my browser it redirects me to...

---

<div class="post-metadata">

**Author:** ![Quincy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ea666f/32.png) [@Quincy](https://www.ruby-forum.com/u/Quincy)\
**Post date:** [July 26, 2011, 2:12pm UTC](https://www.ruby-forum.com/t/port-forwarding-problem/209918/2 "2011-07-26T14:12:47Z")

</div>

On Tue, Jul 26, 2011 at 06:35:23AM -0400, Quincy wrote:

> 2 listen 2222;  
> 13  
> 14 location @mediawiki {  
> 15 rewrite ^/([^?]_)(?:?(._))? /index.php?title=$1&$2 last;  
> 16 }  
> …  
> 44 }

I believe this redirect is sent by mediawiki.  
Could you show debug log of the request

> **[A debugging log](http://nginx.org/en/docs/debugging_log.html)**

?

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Quincy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ea666f/32.png) [@Quincy](https://www.ruby-forum.com/u/Quincy)\
**Post date:** [July 26, 2011, 2:31pm UTC](https://www.ruby-forum.com/t/port-forwarding-problem/209918/3 "2011-07-26T14:31:17Z")

</div>

On Tue, Jul 26, 2011 at 06:35:23AM -0400, Quincy wrote:

Hi there,

> I have two machines. One is server with nginx and the other is NAT.  
> On server I’ve got mediawiki on port 2222. I forwared port 55555 on my  
> nat to server:2222. I have one big problem. When I try to open  
> NAT\_IP:55555 in my browser it redirects me to http://:2222. What’s  
> wrong with my config?

I don’t see a pure-config way to tell nginx to redirect using exactly  
the Host: header sent by the client. I think that that is probably what  
you want to have.

You can “work around” it if you are happy to insist that the client uses  
one specific host:port url to access the service:

> Here is my virtualhost config file:  
> 1 server {  
> 2 listen 2222;  
> 3 server\_name MYNAMEl;

server\_name NAT\_IP:55555;

> 4 server\_name\_in\_redirect off;

server\_name\_in\_redirect on;

> 5 port\_in\_redirect off;

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Quincy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ea666f/32.png) [@Quincy](https://www.ruby-forum.com/u/Quincy)\
**Post date:** [July 26, 2011, 2:59pm UTC](https://www.ruby-forum.com/t/port-forwarding-problem/209918/4 "2011-07-26T14:59:42Z")

</div>

On Tue, Jul 26, 2011 at 04:38:09PM +0400, Igor S. wrote:

> On Tue, Jul 26, 2011 at 01:30:19PM +0100, Francis D. wrote:
> 
> > On Tue, Jul 26, 2011 at 06:35:23AM -0400, Quincy wrote:

Hi there,

> “server\_name\_in\_redirect off” forces to use client Host or, if there is  
> no Host, server IP.

If the client sends

Host: hostname:1234

then “server\_name\_in\_redirect off” causes nginx to redirect using

[http://hostname/](http://hostname/)

i.e., it drops the :port from the Host: header. Unless my testing  
is broken.

It might be convenient to have an option of “use exactly what the  
client sent”, but that won’t be very useful if the client sends a  
broken hostname. ngx\_http\_validate\_host() protects against the broken  
hostname now.

But I don’t have a patch to offer.

> However, this is probably issue with mediawiki.  
> If mediawiki is run via FastCGI, then setting
> 
> fastcgi\_param SERVER\_NAME MYNAMEl;  
> fastcgi\_param SERVER\_PORT 55555;
> 
> may help.

Yes – it would be good to show whether the redirect is coming from  
nginx or mediawiki.

To the original poster: make a directory “temp” in your document root,  
then do “curl -v http://NAT\_IP:55555/temp”, and if your nginx config  
is such that that request will be handled internally within nginx, you  
should see a HTTP 301 redirect to /temp/; if the full Location: header  
shows port 55555, then nginx is processing the redirect as you intend,  
and the original problem is likely best solved via mediawiki.

All the best,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Quincy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ea666f/32.png) [@Quincy](https://www.ruby-forum.com/u/Quincy)\
**Post date:** [July 26, 2011, 2:38pm UTC](https://www.ruby-forum.com/t/port-forwarding-problem/209918/5 "2011-07-26T14:38:41Z")

</div>

On Tue, Jul 26, 2011 at 01:30:19PM +0100, Francis D. wrote:

> I don’t see a pure-config way to tell nginx to redirect using exactly  
> the Host: header sent by the client. I think that that is probably what  
> you want to have.

“server\_name\_in\_redirect off” forces to use client Host or, if there is  
no Host, server IP. However, this is probably issue with mediawiki.  
If mediawiki is run via FastCGI, then setting

fastcgi\_param SERVER\_NAME MYNAMEl;  
fastcgi\_param SERVER\_PORT 55555;

may help.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Quincy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ea666f/32.png) [@Quincy](https://www.ruby-forum.com/u/Quincy)\
**Post date:** [July 27, 2011, 12:00pm UTC](https://www.ruby-forum.com/t/port-forwarding-problem/209918/6 "2011-07-27T12:00:03Z")

</div>

The problem was in mediawiki I think. I changed both ports - now they  
are the same. And everything works from the internet.

Thanks.

Posted at Nginx Forum:

> **[Re: Port forwarding problem](https://forum.nginx.org/read.php?2,212938,212991#msg-212991)**
>
> Hi I have two machines. One is server with nginx and the other is NAT. On server I've got mediawiki on port 2222. I forwared port 55555 on my nat to server:2222. I have one big problem. When I try to open NAT\_IP:55555 in my browser it redirects me to...
