20

What is the difference between Nginx ~ and ~* regexes?

For example:

if ($http_referer ~* www.foobar.net) {
    ...
}

vs

if ($http_referer ~ www.foobar.net) {
    ...
}
PartialOrder
  • 302
  • 1
  • 2
  • 8
  • In your example the `~*` means that `WWW.FOOBAR.NET` as an uppercase referrer would be included in the following ruleset. – Jesse Nickles Aug 28 '22 at 19:18

2 Answers2

36

~: If a tilde modifier is present, this location will be interpreted as a case-sensitive regular expression match.

~*: If a tilde and asterisk modifier is used, the location block will be interpreted as a case-insensitive regular expression match.

cduffin
  • 824
  • 7
  • 8
1

cduffin is correct.

Here is an example of using this regex for a location block to reject uri's that try to access a certain file type (assuming we are using try_files lower in the nginx config)

location ~* \.(txt|log|config)$ {
    return 403;
}
Cory Lewis
  • 121
  • 1