Trailing Slash Redirect Loop Help

classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

Trailing Slash Redirect Loop Help

j94305
Hi,

I am having an issue getting rid of the trailing slashes for directories.  I
have used the following to get rid off the trailing slash:


#rewrite all URIs without any '.' in them that end with a '/'        
        #rewrite ^([^.]*)/$ $1 permanent;

&
#rewrite all URIs that end with a '/'  
rewrite ^/(.*)/$ /$1 permanent;

They both work, but they do not when it comes to directories.  When it is a
directory the page is not displayed because it has different redirects.
Here is the code that I have used inside the LOCATION definition as well as
in the SERVER block and they just do not work.


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

Or, I have used this one:

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

Note:  I have tested individually the two rules either in the server block
or the location block and I still get the redirection problems with files or
directories.


Could any of you please help me and tell me what I am doing wrong.
Basically, when I enable the rewrite rule to get rid off the trailing slash
all directories get multiple redirects and end up in a loop.

Here is the nginx configuration.


user www-data;
worker_processes X;
pid /xxxx.pid;

events {
        worker_connections xxxx;
        # multi_accept on;
}



http {
        ##
        # Basic Settings
        ##

        sendfile off;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        #below hides the version of nginx
        server_tokens off;

        server_names_hash_bucket_size 64;
        #this sets the url hash map table size for url rewrites
        map_hash_bucket_size 128;
        map_hash_max_size 2048;
        # server_name_in_redirect off;

        include /xxxx.types;
        default_type application/octet-stream;

     

        ##
        # Gzip Settings
        ##

        gzip on;

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 7;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
  # Disable for IE < 6 because there are some known problems
        gzip_disable "MSIE [1-6].(?!.*SV1)";

        gzip_types
                text/plain
                text/css
                text/javascript
                application/javascript
                application/json
                application/x-javascript
                application/xml;

        ##
        # Buffer Size
        ##
        proxy_buffering on;
        proxy_buffers   4 256k;
        proxy_buffer_size   128k;
        proxy_busy_buffers_size   256k;

     


        ##
        ## URL REWRITE MAP
        ## File that contains all url rewrite rules for server
        include xxxurlmap.conf;


# SERVER DEFINITIONS




###FORWARD ALL 80 INCOMING REQUEST TO SSL
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name example.com www.example.com;
        return 301 https://www.example.com$request_uri;
}

#HTTPS server

#CHANGE DOMAIN NAME from NO-WWW to WWW - CREATE ONE SERVER INSTANCE AND
RETURN IT
server {
    listen       443 ssl http2;
    server_name  example.com;
    ssl_certificate     /xxx.pem;
    ssl_certificate_key /xxxx.pem;
   return       301 https://www.example.com$request_uri;
}


# Secure Server Configuration HTTPS Server

server {

        listen 443 ssl http2;
        listen [::]:443 ssl http2;        
        server_name www.example.com;

 
        ssl_certificate     /etc/ssl/fullchain.pem;
        ssl_certificate_key /etc/ssl/privkey.pem;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        ssl_session_cache shared:SSL:20m;
        ssl_session_timeout 60m;
       
     
 
       ## THIS CONDITION REWRITES ALL URLS TO THE NEW ONES
        if ( $redirect_uri ) {
        return 301 $redirect_uri;
           }
   

        location / {
            proxy_pass <a href="https://xxxx:xportNumber;">https://xxxx:xportNumber;
            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;
   #this deletes all the traling slash of all Content
   if (!-e $request_filename) {
        rewrite ^/(.*)/$ /$1 permanent;
         }


                }


        }

}

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273964#msg-273964

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Steven Hartland
If is evil I'd suggest using try_files instead, which is typically something like:

location / {
     try_files $uri $uri/ =404;
}

    Regards
    Steve


On 28/04/2017 15:27, Alex Med wrote:
Hi,

I am having an issue getting rid of the trailing slashes for directories.  I
have used the following to get rid off the trailing slash:


#rewrite all URIs without any '.' in them that end with a '/'         
        #rewrite ^([^.]*)/$ $1 permanent;

&
#rewrite all URIs that end with a '/'  
rewrite ^/(.*)/$ /$1 permanent;

They both work, but they do not when it comes to directories.  When it is a
directory the page is not displayed because it has different redirects. 
Here is the code that I have used inside the LOCATION definition as well as
in the SERVER block and they just do not work.


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

Or, I have used this one:

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

Note:  I have tested individually the two rules either in the server block
or the location block and I still get the redirection problems with files or
directories.


Could any of you please help me and tell me what I am doing wrong. 
Basically, when I enable the rewrite rule to get rid off the trailing slash
all directories get multiple redirects and end up in a loop.

Here is the nginx configuration.


user www-data;
worker_processes X;
pid /xxxx.pid;

events {
	worker_connections xxxx;
	# multi_accept on;
}



http {
        ##
        # Basic Settings
        ##

        sendfile off;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        #below hides the version of nginx 
	server_tokens off;

        server_names_hash_bucket_size 64;
        #this sets the url hash map table size for url rewrites
        map_hash_bucket_size 128;
        map_hash_max_size 2048;
	# server_name_in_redirect off;

        include /xxxx.types;
        default_type application/octet-stream;

     

        ##
        # Gzip Settings
        ##

        gzip on;

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 7;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
 	# Disable for IE < 6 because there are some known problems
        gzip_disable "MSIE [1-6].(?!.*SV1)";

        gzip_types
		text/plain
                text/css 
		text/javascript
		application/javascript
	        application/json
		application/x-javascript
		application/xml;

        ##
        # Buffer Size
        ##
        proxy_buffering on;
        proxy_buffers   4 256k;
        proxy_buffer_size   128k;
        proxy_busy_buffers_size   256k;

     


        ##
        ## URL REWRITE MAP
        ## File that contains all url rewrite rules for server
        include xxxurlmap.conf;


# SERVER DEFINITIONS




###FORWARD ALL 80 INCOMING REQUEST TO SSL
server {
	listen 80 default_server;
	listen [::]:80 default_server;
	server_name example.com www.example.com;
	return 301 https://www.example.com$request_uri;
}

#HTTPS server

#CHANGE DOMAIN NAME from NO-WWW to WWW - CREATE ONE SERVER INSTANCE AND
RETURN IT
server {
    listen       443 ssl http2;
    server_name  example.com;
    ssl_certificate     /xxx.pem;
    ssl_certificate_key /xxxx.pem;
   return       301 https://www.example.com$request_uri;
}


# Secure Server Configuration HTTPS Server

server {

        listen 443 ssl http2;
        listen [::]:443 ssl http2;        
        server_name www.example.com;

 
        ssl_certificate     /etc/ssl/fullchain.pem;
        ssl_certificate_key /etc/ssl/privkey.pem;
	    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        ssl_session_cache shared:SSL:20m;
        ssl_session_timeout 60m;
	
     
 
       ## THIS CONDITION REWRITES ALL URLS TO THE NEW ONES
        if ( $redirect_uri ) {
        return 301 $redirect_uri;
           }
   

        location / {
            proxy_pass https://xxxx:xportNumber;
            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;
   #this deletes all the traling slash of all Content 
   if (!-e $request_filename) {
        rewrite ^/(.*)/$ /$1 permanent;
         }


                } 


        }

}

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273964#msg-273964

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
Steveh:

Thank you for your reply.

So what you are suggesting me to do is something like this:

location / {
    try_files $uri $uri/ @rewrite;
}

location @rewrite {
     rewrite ^/(.*)/$ /$1 permanent;
}

Put try files inside the location block and create a new block with the
rewrite?

Thank you for the clarification!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273966#msg-273966

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Steven Hartland
If I understand what you're trying to do correctly, then I think you want something like:
# Ensure no tailing slashes
rewrite ^/(.*)/$ /$1 permanent;

location @upstream {
	proxy_pass https://xxxx:xportNumber;
	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;
}
location / {
	try_files $uri $uri/ @upstream;
}
This will accept /wibble/ and rewrite it to /wibble (this will be passed back to the client which will then re-request /wibble) when the new request rewritten comes it the server will still look for local matches of /wibble (the file) and /wibble/<index files> (the directory) when searching for the content to serve and if not found will pass the request to your upstream server.
   
    Regards
    Steve

On 28/04/2017 15:48, Alex Med wrote:
Steveh:

Thank you for your reply.

So what you are suggesting me to do is something like this:

location / {
    try_files $uri $uri/ @rewrite;
}

location @rewrite {
     rewrite ^/(.*)/$ /$1 permanent;
}

Put try files inside the location block and create a new block with the
rewrite?

Thank you for the clarification!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273966#msg-273966

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
Steve -

Thank you so much this has brought so much clarity!  I appreciate the time
you spend writing the reply.

So the rewrite ^/(.*)/$ /$1 permanent; needs to be outside of the location
definition and inside the server definition, correct?

Infinite  thanks!

Alex

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273969#msg-273969

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Steven Hartland
Yep.

On 28/04/2017 16:43, Alex Med wrote:
Steve -

Thank you so much this has brought so much clarity!  I appreciate the time
you spend writing the reply.

So the rewrite ^/(.*)/$ /$1 permanent; needs to be outside of the location
definition and inside the server definition, correct?

Infinite  thanks!

Alex

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273969#msg-273969

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
Steven -

I implemented your suggestion and I still get the same problem with the
directories ... for anything else it works. But when I try to access a
directory ... I can see on the browser address bar the / appearing and
disappearing and then it finally does but the browser gives this error:

Too many redirects occurred trying to open https://xxxx.com/xxx".  This
might occur if you open a page that is redirected to open another page with
then is redirected to open the original page.

I checked and no my configuration I do not redirect anything to a directory
to give that error.

Thanks for your help!

Alex

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273971#msg-273971

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Francis Daly
In reply to this post by j94305
On Fri, Apr 28, 2017 at 10:27:12AM -0400, Alex Med wrote:

Hi there,

> I am having an issue getting rid of the trailing slashes for directories.  I
> have used the following to get rid off the trailing slash:

Why do you want to get rid of the trailing slash for directories?

You can do it; but you will probably have to write the code to tell your
web server how to handle the request, because the common case is that
the slash is wanted.

So - you can make a request for /file/; nginx can issue a redirect to
/file; you can make a request for /file; and nginx can serve the content
of /usr/local/nginx/html/file. All good.

Next - you make a request for /dir/; nginx can issue a redirect to /dir;
you can make a request for /dir; what do you want to happen next? The
nginx-is-serving-this-from-the-filesystem behaviour is for nginx to issue
a redirect to /dir/. If you want something else to happen, you must
decide what that something else is, and then arrange that it happens,
possibly by config and possibly by coding.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Steven Hartland
In reply to this post by j94305
My guess would be that your app is redirecting back to the slash urls 

Your could test this with a directory on the webserver that has a matching index file.

Alternatively point a browser at the upstream and check for redirects directly

On 28/04/2017 17:52, Alex Med wrote:
Steven -

I implemented your suggestion and I still get the same problem with the
directories ... for anything else it works. But when I try to access a
directory ... I can see on the browser address bar the / appearing and
disappearing and then it finally does but the browser gives this error:

Too many redirects occurred trying to open https://xxxx.com/xxx".  This
might occur if you open a page that is redirected to open another page with
then is redirected to open the original page.

I checked and no my configuration I do not redirect anything to a directory
to give that error.

Thanks for your help!

Alex

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,273971#msg-273971

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
Steve -

You are right something else is adding a trailling slash to directories.  Is
there a way to configure nginx to remove trailing slashes from everything
except from directories?

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,274132#msg-274132

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
In reply to this post by Francis Daly
Francis -

Yes, I am realizing that is a nightmare going against the trailing-slashed
directory nature.  So I am going to have this rule take off slashes from
anything but directories.  Do you have any suggestions as how to do it, but
without "if"

Thank you so much!

Alex

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,274134#msg-274134

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Francis Daly
On Wed, May 10, 2017 at 11:10:36AM -0400, Alex Med wrote:

Hi there,

> Yes, I am realizing that is a nightmare going against the trailing-slashed
> directory nature.  So I am going to have this rule take off slashes from
> anything but directories.  Do you have any suggestions as how to do it, but
> without "if"

There's a few possibly-useful questions to consider:

Why do you want to do this?

As in: what is the problem that you want to solve? Possibly there is a
better approach than this.

Also: given that you want to do this, why do you want to do this without
"if"?

Sometimes "if" is the correct tool to use.

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.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
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 <a href="http://xxx.xx.xx.xxx:IIII;">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 <a href="http://xxx.xx.xx.xxx:IIII;">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

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,285805#msg-285805

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Francis Daly
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 "http://example.com/xxxx" 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 <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;

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 <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;

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

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
Dear Francis:

Thank you for your answers.

Here is the full configuration with reverse proxy, pagespeed,and
lowercasing. It all
works well, but now that I incorporated removing the trailing slash it
brought up new
issues: https://pastebin.com/keQ239D4.  Let me know if you prefer that I
post the configurations
in the post.

Here are the answers to your questions:

As stated before the "evil if" or try_files work well except when it comes
to directories or folders.


>>>>For clarity: can you show the config you use, and one request that
>>>>Fuses that config, and the response that you get? And, in case it is
not
>>>>Fobvious, can you show the response that you want instead?
This is with the "evil if" enable, I get the following response when it is a
directory such as the blog directory:


curl -i http://example.com/blog/
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 09 Oct 2019 15:06:04 GMT
Content-Type: text/html
Content-Length: 178
Location: http://example.com/blog
Connection: keep-alive

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

curl -i http://example.com/blog
HTTP/1.1 301
Server: nginx
Date: Wed, 09 Oct 2019 15:06:48 GMT
Content-Length: 0
Connection: keep-alive
Location: /blog/
Cache-Control: s-maxage=10


If I disable the "evil if" and try with try_files, I get this:

To answer your question:

>>Does the proxy_pass upstream
>>share the same filesystem as this nginx?
No, they do not share the same file system.

curl -i http://example.com/blog/
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 09 Oct 2019 15:13:23 GMT
Content-Type: text/html
Content-Length: 178
Location: http://example.com/blog
Connection: keep-alive

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

curl -i http://example.com/blog
HTTP/1.1 301
Server: nginx
Date: Wed, 09 Oct 2019 15:13:30 GMT
Content-Length: 0
Connection: keep-alive
Location: /blog/
Cache-Control: s-maxage=10

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,285830#msg-285830

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Francis Daly
On Wed, Oct 09, 2019 at 01:54:58PM -0400, Alex Med wrote:

Hi there,

> Here is the full configuration with reverse proxy, pagespeed,and
> lowercasing. It all
> works well, but now that I incorporated removing the trailing slash it
> brought up new
> issues: https://pastebin.com/keQ239D4.  Let me know if you prefer that I
> post the configurations
> in the post.

Thanks for this.

Some of this configuration is not "stock nginx", but I guess that the
extra modules do not affect the issue here.

It would be convenient for history here, to have the config here; but
the relevant part is basically the part shown in the previous mail.

Most requests eventually use a proxy_pass to an upstream server.


> This is with the "evil if" enable, I get the following response when it is a
> directory such as the blog directory:

I suspect that there may be an interpretation issue here.

When I read "the blog directory", I understand something along the lines
of "/usr/local/nginx/html/blog/index.html exists as a file that nginx
can see".

I now suspect that you intend something like "the upstream server
considers it to be a directory".

> curl -i http://example.com/blog/
> HTTP/1.1 301 Moved Permanently
> Server: nginx
> Date: Wed, 09 Oct 2019 15:06:04 GMT
> Content-Type: text/html
> Content-Length: 178
> Location: http://example.com/blog
> Connection: keep-alive

That's from nginx; your config says "if the request ends in / and does
not represent a directory on this filesystem, redirect to no-slash".

> curl -i http://example.com/blog
> HTTP/1.1 301
> Server: nginx
> Date: Wed, 09 Oct 2019 15:06:48 GMT
> Content-Length: 0
> Connection: keep-alive
> Location: /blog/
> Cache-Control: s-maxage=10

That's from your upstream server. Presumably it has decided that "/blog
refers to a directory as far as it is concerned; please request /blog/
instead".

And there is your infinite loop.

So I guess the next question is: what response do you want from nginx?

In this specific case: when you request /blog/, how would you like nginx
to handle that request?

And in your answer, also consider: how might nginx possibly be able
to tell that you want the request handled differently from a request
for /file/?

> >>Does the proxy_pass upstream
> >>share the same filesystem as this nginx?
> No, they do not share the same file system.

"if -d" looks on the filesystem. try_files looks on the filesystem.

If the filesystem does not contain the information on whether "this"
thing is a directory or not, looking on the filesystem will not be able
to give a useful answer.


So, what specific problem are you trying to solve?

If you remove the "remove the slash" configuration from nginx, is there
one request that gets a response that you do not want?

Perhaps fixing the handling of *that* request will get you to a config
that does what you want.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
Dear Francis:

What I am perceiving from your answers is that if nginx can not know with
(!-d or the try_files) that the uri is a directory or a file.

Is there a way to tell nginx that if it gets a "/" from the upstream  to
leave it the way it is?  I guess I will give up the idea of having all urls
without a trailing slash.

Regards,

Alex

P.S. I will look into having the APP remove the trailing slash since it
should know better what it is a directory or a file.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,285872#msg-285872

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

j94305
Dear Francis:

What I am perceiving from your answers is that if nginx can not know with
(!-d or the try_files) that the uri is a directory or a file, there is not
much it can do about not removing slashes from the uris it gets.

Is there a way to tell nginx that if it gets a "/" from the upstream to
leave it the way it is? I guess I will give up the idea of having all urls
without a trailing slash.

Regards,

Alex

P.S. I will look into having the APP remove the trailing slash since it
should know better what it is a directory or a file.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273964,285873#msg-285873

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Trailing Slash Redirect Loop Help

Francis Daly
On Mon, Oct 14, 2019 at 11:06:02PM -0400, Alex Med wrote:

Hi there,

> What I am perceiving from your answers is that if nginx can not know with
> (!-d or the try_files) that the uri is a directory or a file, there is not
> much it can do about not removing slashes from the uris it gets.

Correct. More specifically: there is not much nginx can do about removing
slashes based on whether the uri corresponds to a file or directory on
a server that nginx cannot read.

> Is there a way to tell nginx that if it gets a "/" from the upstream to
> leave it the way it is?

I don't understand the question.

nginx will usually leave things the way they are, unless configured
otherwise. (Aside: some default configuration options are "do change
things".)

If it still matters, can you describe a timeline of what you would like
to have happen?

Something like:

* client requests /thing/
* nginx (does something, perhaps involving the filesystem or a
proxy_pass)?
* nginx responds http 301 to /thing, or responds 200 with some content. Or
maybe responds http 301 to /thing/index?

but include all the words at step 2 that will make it very clear what
your desired end result is.

> I guess I will give up the idea of having all urls
> without a trailing slash.

You can do "no trailing slash" if you want to; but you will have to
configure all of your web servers to do what you want them to when they
get a "trailing slash" url.

I don't think that I know what you want them to do when they get a
"trailing slash" url.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx