58

What is the difference between:

 location = /abc {}

and

 locaton ~ /abc {}
Saurabh
  • 71,488
  • 40
  • 181
  • 244
user650505
  • 3,791
  • 4
  • 17
  • 7
  • 1
    the second looks cooler :P (i think the first matches exact location and the second uses regular exp. but since im not sure i dont post this as an answer) – n00b Mar 08 '11 at 22:28

1 Answers1

125

location = /abc {} matches the exact uri /abc

location ~ /abc is a regex match on the uri, meaning any uri containing /abc, you probably want: location ~ ^/abc for the uri begining with /abc instead

Martin Redmond
  • 13,366
  • 6
  • 36
  • 32
  • 44
    You realise that you don't need a regex to match the start. That's nginx default operation with: `location /abc { ... }` – dalore Jul 19 '16 at 10:02
  • Just a quick "I second" on @dalore's comment. /abc exactly does what ~ ^/abc does (at least with my current version of nginx, 1.10.2 on centos). maybe it changed since this answer was written... further reading [here](http://nginx.org/en/docs/http/request_processing.html) – Félix Adriyel Gagnon-Grenier Mar 30 '17 at 17:37
  • 10
    Quick note, the previous statement is not accurate. `location /abc` will only match routes that begin with `/abc` or `/abc/`. That means it will match `/abc/def` but not `/abcdef`. The regex will match both since it matches on strings, not a routes.. – Érik Desjardins Jun 23 '20 at 16:18
  • 4
    Also `location /abc` will only match when no other location with a regular expression matches. So `location ~ ^/abc` takes precedence over `location /abc` even though it is defined later on: https://www.keycdn.com/support/nginx-location-directive. – Samuel Jun 16 '21 at 07:23