Home > OS >  nginx location regex not matching url
nginx location regex not matching url

Time:07-08

guys ,my nginx location conf file is here:

       location ~  ^(.*)event(.*)$ {
                return 777;
        }

and ,mu url is

aaa.com/index.php?r=event/indexpage&a=1&b=2

but ,its not working , plx help me , thx

CodePudding user response:

The location directive cannot match against the query string.

https://nginx.org/en/docs/http/request_processing.html

Note that locations of all types test only a URI part of request line without arguments. This is done because arguments in the query string may be given in several ways..

You can check the query string inside the location directive though.

location / {
  if ($query_string ~ /^(.*)event(.*)$/) {
    return 777;
  }
}

You can also use a map to perform the match.

map $query_string $has_event {
  ~^(.*)event(.*)$ 1;
  default          0;
}

...

location / {
  if ($has_event) {
    return 777;
  }
}

If you use this method be sure to put the map directive in the right context, it can only live in the http context.

https://nginx.org/en/docs/http/ngx_http_map_module.html

  • Related