PartialOrder Asked: 2016-09-07 06:23:40 +0800 CST2016-09-07 06:23:40 +0800 CST 2016-09-07 06:23:40 +0800 CST What is the difference between Nginx ~ and ~* regexes? 772 What is the difference between Nginx ~ and ~* regexes? For example: if ($http_referer ~* www.foobar.net) { ... } vs if ($http_referer ~ www.foobar.net) { ... } nginx regex 2 Answers Voted Best Answer cduffin 2016-09-07T06:25:17+08:002016-09-07T06:25:17+08:00 ~: 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. Cory Lewis 2020-06-18T07:33:41+08:002020-06-18T07:33:41+08:00 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; }
~: 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 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)