72

What is the meaning of the tilde after the location block in the nginx configuration?

for example

location ~ ^/download/(.*)$ {
  alias /home/website/files/$1;
}

What is the difference between with and without the "~" ?

Chen A.
  • 103
  • 4
mahen3d
  • 3,905
  • 12
  • 34
  • 55
  • http://nginx.org/r/location – Michael Hampton Mar 12 '14 at 04:53
  • A google search for "nginx tilde" gives the answer as the first result... Basic Nginx Configuration – Linode Library https://library.linode.com/web-servers/nginx/configuration/basic‎ Jul 26, 2013 - When a location directive is followed by a tilde (~), nginx performs a regular expression match. These matches are always case-sensitive. – Drew Khoury Mar 12 '14 at 09:34

1 Answers1

77

The tilde instructs nginx to perform a case-sensitive regular expression match, instead of a straight string comparison.

More details in the docs.

EEAA
  • 108,414
  • 18
  • 172
  • 242
  • 1
    Additionally, `~*` modifier for case-insensitive reg exp matching and `^~` modifier to override possible matches elsewhere. – Akseli Palén Nov 20 '20 at 18:51