Trailing Slash Redirect Loop Help

Francis Daly francis at
Tue Oct 8 22:46:04 UTC 2019

On Sun, Oct 06, 2019 at 01:02:22PM -0400, Alex Med wrote:

Hi there,

> So, now what I want is to get rid off the trailing slashes for anything that
> is not a directory or a folder (because my tomcat app seems to add a
> trailing slash to it and that creates an infinite loop that does not allow
> the page to render.).

That sounds like unusual behaviour for a tomcat app.

Can I suggest that you may be better off fixing the app so that it
works correctly?

> I have used many things to address the issue.  You can see my efforts below,
> but they do not work.  I always get the 
> infinite loop for directories and an error from the browser:

For clarity: can you show the config you use, and one request that
uses that config, and the response that you get? And, in case it is not
obvious, can you show the response that you want instead?

With a specific example, it may be clearer what needs to be configured.

> The browser can not open the page "" The error is:
> "Load can not follow more than 20 redirections" (:0)

Rather than "a browser", if you use "curl -v" or "curl -i", it should
be clearer what is happening.

If the response is a 30x redirect to a Location that is different from the
original request, perhaps make a new "curl -i" request for that Location.

At some point in the cycle, the loop will become obvious.

> # 1 - The Evil If
> if ( !-e $request_filename ) { rewrite ^/(.*)/$ /$1 permanent; }

You want "request ends in slash, and does not correspond to a
directory". Use "!-d".

And - that "if" does not look evil to me.

> # 2 - TRY_FILES

>              try_files $uri $uri/ @trimslash; 
>             proxy_pass;

I'm not sure what this is trying to do. Does the proxy_pass upstream
share the same filesystem as this nginx?

> location @trimslash {
>           rewrite ^/(.*)/$ /$1 permanent;
>             proxy_pass;

Once the "rewrite" happens, the "proxy_pass" will not apply. So I'm not
sure what this is trying to do, either.

Francis Daly        francis at

More information about the nginx mailing list