Welcome! Log In Create A New Profile

Advanced

Re: Why 301 permanent redirect with appended slash?

J. Lewis Muir
July 31, 2019 12:46PM
On 07/31, Francis Daly wrote:
> On Tue, Jul 30, 2019 at 05:12:01PM -0500, J. Lewis Muir wrote:
>
> Hi there,
>
> > I have a minimal nginx.conf with one server block that sets the root
> > directory and one location with a prefix string of "/foo/", and for a
> > request of "/foo", it returns a 301 permanent redirect to "/foo/". Why?
> > I expected it to return 404 or similar. I also tried a prefix string of
> > "/foo", but that also results in the same 301.
>
> I get a 301 if the directory "foo" exists; and a 404 if it does not.
>
> Do you get something different?

No, I get exactly what you described. Sorry, I failed to state in my
initial email that the directory "foo" exists (as well as the file
"foo/index.html" which probably doesn't matter here).

> > But in my case, I don't believe the request is being processed by any of
> > those *_pass directives.
>
> If you request "/directory", and "directory" exists, then the filesystem
> handler will issue a 301 to "/directory/", which I think is what you
> are seeing.

Indeed.

> As in: your request for "/foo" does not match any location{}, and so is
> handled at server-level, which runs the filesystem handler and returns
> 200 if the file "foo" exists, 301 if the directory "foo" exists, and
> 404 otherwise.

Yes, thank you very much for the explanation! That all makes sense.
I couldn't find this behavior documented anywhere; is it documented
somewhere that I've missed?

> Change your config to be
>
> location /foo/ { return 200 "location /foo/\n"; }
>
> and you will see when that location is used.

Nice; that's useful for testing!

> If your config has
>
> location /foo {}
>
> then a similar consideration applies, except the request "/foo" is now
> handled in that location which, per the above configuration, uses the
> filesystem handler.

Understood.

Thanks!

Lewis
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Why 301 permanent redirect with appended slash?

J. Lewis Muir July 30, 2019 06:12PM

Re: Why 301 permanent redirect with appended slash?

Francis Daly July 31, 2019 08:06AM

Re: Why 301 permanent redirect with appended slash?

J. Lewis Muir July 31, 2019 12:46PM

Re: Why 301 permanent redirect with appended slash?

Francis Daly August 01, 2019 12:56PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 107
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