Trailing slash rewrite help

Kurt Hansen khansen at charityweb.net
Sat Jul 18 09:28:12 MSD 2009


Hello,

I'm using nginx as a reverse proxy to a mod_perl backend server and am 
having trouble with my rewrite rule in the case of a missing trailing 
slash on a subdirectory.

For example, I want

www.example.com/action mapped to the example/action/ directory on the 
mod_perl server.

However, what happens is that www.example.com/action is mapped to 
example/example/action/

Here are  the relevant location directives where the rewrite is done:

     location /example/ {
        # root   html;
        index  index.htm;

        rewrite  ^/example(.*)/$ /example/$1/index.htm break;

        proxy_redirect off;
        proxy_pass http://cweb;
  }


     location / {
        # root   html;
        index  index.htm;

        rewrite  ^/$ /example/index.htm break;

        rewrite  ^/(.*) /example/$1 break;

        proxy_pass http://cweb;
    }

I think what is happening is that the rewrite comes out as 
/example/action then nginx adds the trailing slash and it gets run 
through the re-write again. I added the /example/ location directive so 
at least the right pages are displayed, but the URL comes back to the 
browser as www.example.com/example/action/

The nginx server is also an NFS server so the files reside on the nginx 
server. I've tried an if (-d $request_filename) but that doesn't work.

Any help would be greatly appreciated.

Thanks,

Kurt Hansen






More information about the nginx mailing list