19

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

For example:

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

vs

if ($http_referer ~ www.foobar.net) {
    ...
}
PartialOrder
  • 292
  • 1
  • 2
  • 8

2 Answers2

31

~: 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
  • 774
  • 6
  • 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
  • 21
  • 1