Why 301 permanent redirect with appended slash?

J. Lewis Muir jlmuir at imca-cat.org
Wed Jul 31 16:45:58 UTC 2019


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


More information about the nginx mailing list