Nginx location “not equal to” regex

后端 未结 2 487
别那么骄傲
别那么骄傲 2020-12-02 14:17

How do I set a location condition in Nginx that responds to anything that isn\'t equal to the listed locations?

I tried:

location !~/(         


        
2条回答
  •  醉酒成梦
    2020-12-02 14:42

    i was looking for the same. and found this solution.

    Use negative regex assertion:

    location ~ ^/(?!(favicon\.ico|resources|robots\.txt)) { 
    .... # your stuff 
    } 
    

    Source Negated Regular Expressions in location

    Explanation of Regex :

    If URL does not match any of the following path

    example.com/favicon.ico
    example.com/resources
    example.com/robots.txt
    

    Then it will go inside that location block and will process it.

提交回复
热议问题