# Port redirection issue while using ssh tunnel

**URL:** <https://www.ruby-forum.com/t/port-redirection-issue-while-using-ssh-tunnel/235899>\
**Category:** NGINX\
**Created:** [July 23, 2013, 11:23am UTC](https://www.ruby-forum.com/t/port-redirection-issue-while-using-ssh-tunnel/235899 "2013-07-23T11:23:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [July 23, 2013, 11:23am UTC](https://www.ruby-forum.com/t/port-redirection-issue-while-using-ssh-tunnel/235899/1 "2013-07-23T11:23:32Z")

</div>

Hi list,

I have a web app proxied by nginx. Everything works fine locally.  
However the web server is on our private network and I would like to  
access it though a ssh tunnel from the outside.  
Most operations works fine except when the web app returns a 302  
redirection. In that case it seems that nginx removes the http port  
(detailed issue below).

Here are the details:  
The ssh tunnel is made through our ssh gateway:  
ssh removed\_email\_address@domain.invalid -L8080:privateWebServer:80

I then connect to a ‘normal’ page and everything looks good:  
$ curl [http://localhost:8080/wiki/wiki](http://localhost:8080/wiki/wiki) -D -

HTTP/1.1 200 OK  
Server: nginx/0.7.67  
Date: Tue, 23 Jul 2013 08:52:54 GMT  
Content-Type: text/html; charset=UTF-8  
Connection: keep-alive  
Content-Length: 98961

$ curl [http://localhost:8080/wiki](http://localhost:8080/wiki) -D -  
However when I try a page that returns a 302 redirect, I have this:  
HTTP/1.1 302 Found  
Server: nginx/0.7.67  
Date: Tue, 23 Jul 2013 08:54:46 GMT  
Content-Type: text/html; charset=UTF-8  
Connection: keep-alive  
Content-Length: 93937  
Location: [http://localhost/wiki/wiki](http://localhost/wiki/wiki)

(the 8080 port was removed from the location).

If I try to contact directly the web app throug the tunnel (on port  
6544) I have this:  
$ curl [http://localhost:6544/wiki](http://localhost:6544/wiki)  
HTTP/1.1 302 Found  
Content-Length: 178  
Content-Type: text/html; charset=UTF-8  
Date: Tue, 23 Jul 2013 08:57:16 GMT  
Location: [http://localhost:6544/wiki/wiki](http://localhost:6544/wiki/wiki)  
Server: waitress

So it looks like that the problem comes from my nginx configuration.

location /wiki {

```
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header X-Forwarded-Proto $scheme;  
port\_in\_redirect on;  
proxy\_pass [http://127.0.0.1:6544](http://127.0.0.1:6544);

```
    }

```

I actived the debug log and this extract looks interesting:

2013/07/23 11:08:10 [debug] 14798#0: \*6845 http header: “Host:  
localhost:8080”  
[…]  
2013/07/23 11:08:10 [debug] 14798#0: \*6845 http proxy header:  
2013/07/23 11:08:10 [debug] 14798#0: \*6845 http script copy: "Host: "  
2013/07/23 11:08:10 [debug] 14798#0: \*6845 http script var: “localhost”  
2013/07/23 11:08:10 [debug] 14798#0: \*6845 http script copy: "  
[…]  
"GET /wiki HTTP/1.0  
Host: localhost  
X-Real-IP: _our\_ssh\_gateway\_ip_  
X-Forwarded-For: _our\_ssh\_gateway\_ip_  
X-Forwarded-Proto: http  
Connection: close  
User-Agent: curl/7.26.0  
Accept: _/_

"

I would really appreciate any help regarding this issue.

Regards,  
Adrien

---

<div class="post-metadata">

**Author:** ![Adrien\_Saladin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9e5f3/32.png) [@Adrien\_Saladin](https://www.ruby-forum.com/u/Adrien_Saladin)\
**Post date:** [July 23, 2013, 12:50pm UTC](https://www.ruby-forum.com/t/port-redirection-issue-while-using-ssh-tunnel/235899/2 "2013-07-23T12:50:36Z")

</div>

Hi again,

I found the problem: I had `proxy_set_header Host $host;` in  
the configuration file.  
If I replace this line by `proxy_set_header Host $http_host;` the port is now correctly set on http 302.

Regards,
