Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring nginx to return a 404 when a URL matches a pattern

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

like image 898
shanqn Avatar asked Jan 12 '11 02:01

shanqn


People also ask

Why is Nginx returning 404?

Essentially, the “404 error” indicates that your or your visitor's web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.

What is $scheme in nginx?

The rewritten URL uses two NGINX variables to capture and replicate values from the original request URL: $scheme is the protocol (http or https) and $request_uri is the full URI including arguments. For a code in the 3xx series, the url parameter defines the new (rewritten) URL.


2 Answers

location /test/ {   return 404; } 
like image 145
Vicheanak Avatar answered Oct 19 '22 23:10

Vicheanak


Need to add "^~" to give this match a higher priority than regex location blocks.

location ^~ /test/ {   return 404; } 

Otherwise you will be in some tricky situation. For example, if you have another location block such as

location ~ \.php$ {   ... } 

and someone sends a request to http://your_domain.com/test/bad.php, that regex location block will be picked by nginx to serve the request. Obviously it's not what you want. So be sure to put "^~" in that location block!

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

like image 23
Chuan Ma Avatar answered Oct 20 '22 00:10

Chuan Ma