# Trailing slash at end URL with nginx as a reverse proxy for multiple website

**URL:** <https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055>\
**Category:** NGINX\
**Created:** [November 20, 2011, 9:12am UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055 "2011-11-20T09:12:04Z")\
**Posts on this page:** 9\
**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 20, 2011, 9:12am UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/1 "2011-11-20T09:12:04Z")

</div>

Hi,

I’m running OpenIndiana 151a server then now I’d like to build the web  
service with nginx. Briefly, my scenario is: one central server  
installed a  
few website in virtual machines, and there is a different real machine  
that  
stores static contents. I want nginx in central server will be the  
reverse  
proxy (get idea from  
[Using Nginx as reverse proxy | Bradley Wright](http://tumblr.intranation.com/post/766288369/using-nginx-reverse-proxy)).

For example, if someone open website [a.com](http://a.com), it will be redirected to  
10.0.0.2:8001, and if other go to [b.com](http://b.com) it will be redirected to  
10.0.0.3:8001, etc …

server {  
listen 80;  
server\_name [a.com](http://a.com);

```
location / {
    proxy_pass http://10.0.0.2:8001;
    proxy_redirect off;
    server_name_in_redirect off;
}

```

}

Nginx works fine, but I don’t know how to rewrite/redirect the URL looks  
like: [http://a.com/something](http://a.com/something) becomes [http://a.com/something/](http://a.com/something/)

If someone open the link [http://a.com/something](http://a.com/something) nginx can’t serve it  
then  
return the error message that says “[http://a.com:8001/something](http://a.com:8001/something)”  
unreachable.

If you have any advice, please tell. It’s highly appreciated.

Thanks,

---

<div class="post-metadata">

**Author:** ![nhatbk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@nhatbk](https://www.ruby-forum.com/u/nhatbk)\
**Post date:** [November 20, 2011, 9:36am UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/2 "2011-11-20T09:36:40Z")

</div>

On 20 Nov 2011 08h11 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 10.0.0.2:8001, and if other go to [b.com](http://b.com) it will be redirected to  
> }  
> }
> 
> Nginx works fine, but I don’t know how to rewrite/redirect the URL  
> looks like: [http://a.com/something](http://a.com/something) becomes [http://a.com/something/](http://a.com/something/)
> 
> If someone open the link [http://a.com/something](http://a.com/something) nginx can’t serve it  
> then return the error message that says  
> “[http://a.com:8001/something](http://a.com:8001/something)” unreachable.
> 
> If you have any advice, please tell. It’s highly appreciated.

If it’s rewriting all the URIs so that they have a trailing slash then  
try this:

1. At the http level:

map $request\_uri $no\_trailing\_slash {  
default 1;  
~.\*[^/]$ 0;  
}

1. At the server level (vhost):

server {  
listen 80;  
server\_name [a.com](http://a.com);

```
location / {
    if ($no_trailing_slash) {
        return 302 $request_uri/;
    }

    proxy_pass http://10.0.0.2:8001;
    proxy_redirect off;
    server_name_in_redirect off;
}

```

}

— appa

---

<div class="post-metadata">

**Author:** ![nhatbk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@nhatbk](https://www.ruby-forum.com/u/nhatbk)\
**Post date:** [November 20, 2011, 9:40am UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/3 "2011-11-20T09:40:59Z")

</div>

On 20 Nov 2011 08h11 WET, removed\_email\_address@domain.invalid wrote:

Oops it’s the other way around.

If it’s rewriting all the URIs so that they have a trailing slash then  
try this:

1. At the http level:

map $request\_uri $no\_trailing\_slash {  
default 0;  
~.\*[^/]$ 1;  
}

1. At the server level (vhost):

server {  
listen 80;  
server\_name [a.com](http://a.com);

```
location / {
    if ($no_trailing_slash) {
        return 302 $request_uri/;
    }

    proxy_pass http://10.0.0.2:8001;
    proxy_redirect off;
    server_name_in_redirect off;
}

```

}

— appa

---

<div class="post-metadata">

**Author:** ![nhatbk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@nhatbk](https://www.ruby-forum.com/u/nhatbk)\
**Post date:** [November 20, 2011, 10:17am UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/4 "2011-11-20T10:17:20Z")

</div>

On Sun, Nov 20, 2011 at 03:11:27PM +0700, Nguyen Hai N. wrote:

> 10.0.0.2:8001, and if other go to [b.com](http://b.com) it will be redirected to  
> }

- 

```
   proxy_redirect off;

```

- 

```
   server_name_in_redirect off;

```

- 

```
   proxy_redirect http://a.com:8001/ /;

```

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![nhatbk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@nhatbk](https://www.ruby-forum.com/u/nhatbk)\
**Post date:** [November 20, 2011, 10:13am UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/5 "2011-11-20T10:13:00Z")

</div>

On Sun, Nov 20, 2011 at 3:11 PM, Nguyen Hai N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If someone open the link [http://a.com/something](http://a.com/something) nginx can’t serve it then  
> return the error message that says “[http://a.com:8001/something](http://a.com:8001/something)”  
> unreachable.

Are you sure it’s nginx error and not backend’s?

Also you may want to try adding these lines:

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

```

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![nhatbk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@nhatbk](https://www.ruby-forum.com/u/nhatbk)\
**Post date:** [November 20, 2011, 2:58pm UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/6 "2011-11-20T14:58:19Z")

</div>

Thank you guys for helping.

I’ve tried following of Antonio, Edho and Igor’s advices but no thing  
changes.

@Igor: If I add proxy\_redirect [http://a.com:8001/](http://a.com:8001/) /; it will return  
error 404 Not Found.

I’m curious in why when I type [http://a.com/something](http://a.com/something) it’s redirected  
to [http://a.com:8001/something](http://a.com:8001/something), so maybe it’s wrong at reverse proxy  
point.

Configuration of reverse proxy as seen:

http {

include /usr/nginx/conf/proxy.conf;

```
server {
    listen 8080;
    server_name abc.com;

```

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\_max\_temp\_file\_size 0;  
}  
}

In proxy.conf

server {  
listen 80;  
server\_name [a.com](http://a.com);

```
access_log on;
error_log on;

location / {
    proxy_pass http://10.2.176.21:8001/;
    #proxy_redirect off;
    #server_name_in_redirect on;

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

```

}  
}

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![nhatbk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@nhatbk](https://www.ruby-forum.com/u/nhatbk)\
**Post date:** [November 20, 2011, 8:07pm UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/7 "2011-11-20T20:07:32Z")

</div>

Remove the trailing / from proxy\_pass directive.

---

<div class="post-metadata">

**Author:** ![nhatbk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@nhatbk](https://www.ruby-forum.com/u/nhatbk)\
**Post date:** [November 21, 2011, 2:49am UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/8 "2011-11-21T02:49:44Z")

</div>

Hi Calin,

It’s removed and still no change.

Thanks,

---

<div class="post-metadata">

**Author:** ![nhatbk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed655f/32.png) [@nhatbk](https://www.ruby-forum.com/u/nhatbk)\
**Post date:** [November 21, 2011, 8:06am UTC](https://www.ruby-forum.com/t/trailing-slash-at-end-url-with-nginx-as-a-reverse-proxy-for-multiple-website/214055/9 "2011-11-21T08:06:30Z")

</div>

I’ve found that TCP port 8001 was used as VCOM tunnel. Then change nginx  
to listen to other port works fine.

Thanks for your kind support.
