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

纵饮孤独 提交于 2019-12-20 09:32:22

问题


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?


回答1:


location /test/ {
  return 404;
}



回答2:


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




回答3:


location ^~ /test/ {
    internal;
}


来源:https://stackoverflow.com/questions/4664872/configuring-nginx-to-return-a-404-when-a-url-matches-a-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!