# Nginx redirect problem

**URL:** <https://www.ruby-forum.com/t/nginx-redirect-problem/204884>\
**Category:** NGINX\
**Created:** [March 24, 2011, 2:27am UTC](https://www.ruby-forum.com/t/nginx-redirect-problem/204884 "2011-03-24T02:27:13Z")\
**Posts on this page:** 2\
**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:** [March 24, 2011, 2:27am UTC](https://www.ruby-forum.com/t/nginx-redirect-problem/204884/1 "2011-03-24T02:27:13Z")

</div>

here is the conf:

```
server {
    listen 10.68.150.4:82;
    server_name h.test.com;
    root /usr/www;

    location / {
        index index.html index.htm index.php;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/local/www/nginx-dist;
    }

    location ~ \.php$ {
            fastcgi_pass unix:/tmp/php-fastcgi.sock;
            include fastcgi_params;
            fastcgi_index index.php;
    }
}

```

I have a router in the front,and have set a port map

67.xx.xx.xx:8081–\>10.68.150.4:82

But there is a problem  
When I visit [http://h.test.com:8081/house,the](http://h.test.com:8081/house,the) redirected to  
[http://h.test.com/house/](http://h.test.com/house/).  
How can make it redirct [http://h.test.com:8081/house/](http://h.test.com:8081/house/) when visit  
[http://h.test.com:8081/house](http://h.test.com:8081/house)?  
TKS.

---

<div class="post-metadata">

**Author:** ![lhmwzy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90db22/32.png) [@lhmwzy](https://www.ruby-forum.com/u/lhmwzy)\
**Post date:** [March 24, 2011, 9:24am UTC](https://www.ruby-forum.com/t/nginx-redirect-problem/204884/2 "2011-03-24T09:24:21Z")

</div>

On Thu, Mar 24, 2011 at 09:26:42AM +0800, lhmwzy wrote:

> ```
> }
> 
> ```
> 
> I have a router in the front,and have set a port map
> 
> 67.xx.xx.xx:8081–\>10.68.150.4:82
> 
> But there is a problem  
> When I visit [http://h.test.com:8081/house,the](http://h.test.com:8081/house,the) redirected to  
> [http://h.test.com/house/](http://h.test.com/house/).  
> How can make it redirct [http://h.test.com:8081/house/](http://h.test.com:8081/house/) when visit  
> [http://h.test.com:8081/house](http://h.test.com:8081/house)?

You can try the following:

```
 server {
     listen 10.68.150.4:82;
     server_name h.test.com:8081 h.test.com;

     location / {
          server_name_in_redirect on;
          port_in_redirect off;
          ...

```

“server\_name\_in\_redirect on” forces to use a fictitious first  
server\_name  
“h.test.com:8081” in a redirect. “port\_in\_redirect off” disables  
a real 82 port in redirect. If you omit port\_in\_redirect you will get

http://h.test.com:8081:82/house/

–  
Igor S.

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