Why 301 permanent redirect with appended slash?

Francis Daly francis at daoine.org
Wed Jul 31 12:05:27 UTC 2019


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?

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

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.

Change your config to be

  location /foo/ { return 200 "location /foo/\n"; }

and you will see when that location is used.

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.

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list