Don't process requests containing folders

Francis Daly francis at
Mon Sep 12 21:49:30 UTC 2016

On Mon, Sep 12, 2016 at 01:55:35PM -0700, Grant wrote:

Hi there,

> > If you want to match "requests with a second slash", do just that:
> >
> >   location ~ ^/.*/ {}
> >
> > (the "^" is not necessary there, but I guess-without-testing that
> > it helps.)
> When you say it helps, you mean for performance?

Yes - I guess that anchoring this regex at a point where it will always
match anyway, will do no harm.

> > If you want to match "requests without a second slash", you could do
> >
> >   location ~ ^/[^/]*$ {}
> >
> > but I suspect you'll be better off with the positive match, plus a
> > "location /" for "all the rest".
> I want to keep my location blocks to a minimum so I think I should use
> the following as my last location block which will send all remaining
> good requests to my backend:
> location ~ (^/[^/]*|.html)$ {}

Yes, that should do what you describe.

Note that the . is a metacharacter for "any one"; if you really want
the five-character string ".html" at the end of the request, you should
escape the . to \.

> And let everything else match the following, most of which will 404 (cheaply):
> location / { internal; }

Testing and measuring might show that "return 404;" is even cheaper than
"internal;" in the cases where they have the same output. But if there
are cases where the difference in output matters, or if the difference
is not measurable, then leaving it as-is is fine.


Francis Daly        francis at

More information about the nginx mailing list