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

前端 未结 3 412
孤街浪徒
孤街浪徒 2021-02-02 05:12

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?

相关标签:
3条回答
  • 2021-02-02 05:38
    location ^~ /test/ {
        internal;
    }
    
    0 讨论(0)
  • 2021-02-02 05:44
    location /test/ {
      return 404;
    }
    
    0 讨论(0)
  • 2021-02-02 05:57

    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

    0 讨论(0)
提交回复
热议问题