Configuring nginx to return 404 when the url matches the pattern

I want nginx to return 404 code when it receives a request that matches the pattern, e.g. /test/* . How can I configure nginx for this?

+42
webserver nginx
Jan 12 2018-11-11T00:
source share
3 answers
 location /test/ { return 404; } 
+65
Jan 13 2018-11-11T00:
source share

You must add “^ ~” to give this match a higher priority than the regular expression location blocks.

 location ^~ /test/ { return 404; } 

Otherwise, you will encounter some difficult situation. For example, if you have another location block, for example

 location ~ \.php$ { ... } 

and someone sends a request http://your_domain.com/test/bad.php , this block of the location of the regular expression will be selected by nginx to serve the request. Obviously, this is not what you want. So be sure to put "^ ~" in this location block!

Link: http://wiki.nginx.org/HttpCoreModule#location

+16
Mar 18 '13 at 16:13
source share
 location ^~ /test/ { internal; } 
+5
Jan 12 2018-11-12T00:
source share



All Articles