Nginx location “not equal to” regex

后端 未结 2 486
别那么骄傲
别那么骄傲 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 15:00

    According to nginx documentation

    there is no syntax for NOT matching a regular expression. Instead, match the target regular expression and assign an empty block, then use location / to match anything else

    So you could define something like

    location ~ (dir1|file2\.php) { 
        # empty
    }
    
    location / {
        rewrite ^/(.*) http://example.com/$1 permanent; 
    }
    

提交回复
热议问题