Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx location "not equal to" regex

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

I tried:

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

But it doesn't trigger the redirect. It simply handles the requested URI using the rules in the rest of the server configuration.

like image 978
Christiaan Avatar asked Apr 30 '13 14:04

Christiaan


Video Answer


1 Answers

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;  } 
like image 193
Déjà vu Avatar answered Sep 18 '22 14:09

Déjà vu