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

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).

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

server {
listen 80;
server_name 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 becomes http://a.com/something/

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

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

Thanks,

On 20 Nov 2011 08h11 WET, [email protected] wrote:

10.0.0.2:8001, and if other go to 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 becomes http://a.com/something/

If someone open the link http://a.com/something nginx can’t serve it
then return the error message that says
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;

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

On 20 Nov 2011 08h11 WET, [email protected] 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;

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

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 it will be redirected to
}

  •    proxy_redirect     off;
    
  •    server_name_in_redirect off;
    
  •    proxy_redirect     http://a.com:8001/  /;
    


Igor S.

On Sun, Nov 20, 2011 at 3:11 PM, Nguyen Hai N. [email protected] wrote:

If someone open the link http://a.com/something nginx can’t serve it then
return the error message that says “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

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/ /; it will return
error 404 Not Found.

I’m curious in why when I type http://a.com/something it’s redirected
to 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;

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.

Remove the trailing / from proxy_pass directive.

Hi Calin,

It’s removed and still no change.

Thanks,

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.