Home > Blockchain >  Nginx negative rewrite location rules
Nginx negative rewrite location rules

Time:08-02

URLs:

1) /store/doc/some_file.doc
2) /store/doc/some_dir/some_file.doc
3) /store/img/some_file.doc
4) /store/img/some_dir/some_file.doc
5) /store/xxx/some_file.doc
6) /store/xxx/some_dir/some_file.doc

Nginx conf:

location /storage/ {
   location ^~/storage/[^(doc|img)]/(.*) {
       return 418;
   }
   rewrite ^/storage/(doc|img)/(.*) /storage/_storage.php?type=$1&file=$2 break;
}

What i want to do:

  1. all URLs what started by /storage/ need specific processing
  2. if URL doesn't match with /storage/doc/* or /storage/img/* - return "I'm reapod" code
  3. if URL match with /storage/doc/* or /storage/img/* - send this request to special file for processing (/storage/_storage.php) with additional GET-param in URL (first param - type [doc or img], second param - URL part after /storage/doc/ or /storage/img/)

For 1-4 URLs all work fine, but for 5 and 6 URLs doesn't send 418.

I don't understand how to write negative location condition?

CodePudding user response:

Found solution:

location /storage/ {
   location ~/storage/[^(doc|img)] { # <--- All what not start by doc or img will be passed
       return 418;
   }
   rewrite ^/storage/(doc|img)/(.*) /storage/_storage.php?type=$1&file=$2 break;
}
  • Related