stop automatic trailing slash addition

Francis Daly francis at daoine.org
Sun Dec 14 10:52:47 UTC 2014


On Sat, Dec 13, 2014 at 03:53:32PM -0800, neubyr wrote:
> On Sat, Dec 13, 2014 at 12:00 AM, Francis Daly <francis at daoine.org> wrote:
> > On Fri, Dec 12, 2014 at 04:00:29PM -0800, neubyr wrote:

Hi there,

> > >       location /test {
> > >           root         /usr/share/nginx/test;
> > >         }
> > >
> > >          location /test/ {
> > >            root         /usr/share/nginx/test-slash;
> > >            try_files $uri default.txt;
> > >         }

> > What request do you make? (Presumably something like "curl -i
> > http://localhost/test" or "curl -i http://localhost/testA")
> >
> > What response do you get? (A http redirect? Or perhaps the content of
> > a particular file on your filesystem?)
> >
> > What response do you want? (The content of a different file on your
> > filesystem? Name the files, so it is clear where the expectation and
> > the result are different)

Those questions still need answers, if you want someone else to be able
to tell you how to configure nginx to do your non-standard thing. (If
such a thing is even possible without extra coding.)

The "standard" thing is: requests that map directly to the filesystem
that match a directory that omit a trailing slash get a http 301 response
to include the trailing slash. That is usually what is wanted.

If you want something else, you'll have to be very clear about what it
is that you want.

The machine will do exactly what you configure it to do, not what you
hope it will do.

> It seems like nginx is adding slash as uri name matches with corresponding
> directory and not file name. I thought nginx will return 404 in this case,
> but it adds trailing slash when matching directory is found.

I'm not sure why you expected a 404 if a non-slash request names an
existing directory by default. That's not the common thing to want.

You can get that by using (for example)

  try_files $uri =404;

but that probably will not do what you want if the request *does* end
in a slash.

> After adding trailing slash uri becomes /test/ and hence it matches next
> location block.

One request is handled in one location.

Depending on what actual configuration you used, the "/test/" url might
count as a new subrequest and start a new location search, or it
might not. See the documentation for whatever directive you are using
(explicitly or implicitly.)

For example using,

  try_files $uri $uri/index.html =404;

in both /test and /test/ locations, a request for /test would send you
the contents of the file

  /usr/share/nginx/test/test/index.html

if it exists (unless other configuration says not to), while a request
for /test/ would send you the contents of the file

  /usr/share/nginx/test-slash/test/index.html

if it exists.

(Your "try_files $uri default.txt;" is unlikely to give you a useful result.)

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list