October 06, 2019 12:24PM
Dear Francis:

After so many years, I am back to the same questions I had many years ago, but never resolved on Nginx. So perhaps, it is time to change this and find a useful implementation.

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

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:

The browser can not open the page "http://example.com/xxxx" The error is: "Load can not follow more than 20 redirections" (:0)

As you stated it:

"And: what's a file and what's a directory? Your initial config
example used proxy_pass, which refers to remote urls, not files
or directories. *This* nginx does not know whether an upstream url
corresponds to a file or to something else. So that may want to be
considered before designing the solution."

What can I do?

Thank you for your feedback!




----this is what I have tried -----


# 1 - The Evil If

if ( !-e $request_filename ) { rewrite ^/(.*)/$ /$1 permanent; }



# 2 - TRY_FILES


location / {

##TRY_FILES WILL GET A $URI AND TEST IF IT IS A FILE AND SERVE IT IF THE FILE EXISTS
##ELSE IT WILL TEST IF THE URI IS A DIRECTORY $URI/ IF DIRECTORY EXISTS IT WILL SERVE IT, AND ELSE AND FINALLY
##IF IT IS NOT EITHER IT WILL PASS IT TO A LOCATION
##THAT WILL REWRITE TO LOWERCASE
## root - OUR APP REQUIRES NO ROOT FOR NGINX TO WORK WITH IT SO NO ROOT IS PROVIDED FROM THE TOMCAT APP.



try_files $uri $uri/ @trimslash;

proxy_pass http://xxx.xx.xx.xxx:IIII;
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_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_redirect off;

###
### ADD THIS FOR WEBSOCKET SUPPORT
###

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

}

location @trimslash {

rewrite ^/(.*)/$ /$1 permanent;

proxy_pass http://xxx.xx.xx.xxx:IIII;
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_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_redirect off;

###
### ADD THIS FOR WEBSOCKET SUPPORT
###

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}

}
##BETA EXAMPLE HTTP SERVER CONFIGURATION END
Subject Author Posted

Trailing Slash Redirect Loop Help

Alex Med April 28, 2017 10:27AM

Re: Trailing Slash Redirect Loop Help

steveh April 28, 2017 10:42AM

Re: Trailing Slash Redirect Loop Help

Alex Med April 28, 2017 10:48AM

Re: Trailing Slash Redirect Loop Help

steveh April 28, 2017 11:36AM

Re: Trailing Slash Redirect Loop Help

Alex Med April 28, 2017 11:43AM

Re: Trailing Slash Redirect Loop Help

steveh April 28, 2017 11:48AM

Re: Trailing Slash Redirect Loop Help

Alex Med April 28, 2017 12:52PM

Re: Trailing Slash Redirect Loop Help

steveh May 01, 2017 06:42PM

Re: Trailing Slash Redirect Loop Help

Alex Med May 10, 2017 10:43AM

Re: Trailing Slash Redirect Loop Help

Francis Daly April 29, 2017 08:36AM

Re: Trailing Slash Redirect Loop Help

Alex Med May 10, 2017 11:10AM

Re: Trailing Slash Redirect Loop Help

Francis Daly May 12, 2017 05:26PM

Re: Trailing Slash Redirect Loop Help

Alex Med October 06, 2019 12:24PM

Re: Trailing Slash Redirect Loop Help

Francis Daly October 08, 2019 06:48PM

Re: Trailing Slash Redirect Loop Help

Alex Med October 09, 2019 11:22AM

Re: Trailing Slash Redirect Loop Help

Francis Daly October 09, 2019 05:34PM

Re: Trailing Slash Redirect Loop Help

Alex Med October 14, 2019 11:04PM

Re: Trailing Slash Redirect Loop Help

Alex Med October 14, 2019 11:06PM

Re: Trailing Slash Redirect Loop Help

Francis Daly October 15, 2019 10:36AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 169
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready