How to match question mark “?” as regexp on nginx.conf location

前端 未结 1 667
失恋的感觉
失恋的感觉 2021-02-04 04:47

I\'d like to match question mark \"?\" as regexp on nginx.conf location.

For example, a URL pattern which I\'d like to match is /something?foo=5 or /something?bar=8 (par

1条回答
  •  清歌不尽
    2021-02-04 05:33

    nginx location block doesn't match query string at all. So it's impossible.

    Location

    This directive allows different configurations depending on the URI.

    In nginx, there is a built-in variable $uri, which the location block is matched against. For example, give a request

    http://www.example.com/app/login.php?username=xyz&password=secret
    

    the $uri value is this string:

     /app/login.php
    

    and the query_string is stored in nginx variable $args:

    username=xyz&password=secret
    

    To do something wrt. query string, you can do something like

    if ($args ~ username=xyz) {
       # do something for requests with this query string
    }
    

    But be careful, IF is Evil

    0 讨论(0)
提交回复
热议问题