location [=|~|~*|^~] /uri/ { … }
= strict match. If this query matches, the search will stop and the request will be processed immediately.
~ is case-sensitive matching (regular expressions can be used)
!~ is case-sensitive mismatching
~* is case-insensitive matching (regular expressions can be used)
!~* is case-insensitive mismatching
^~ This prefix is used for a regular string, then tells nginx not to test the regular expression if the path matches
Example:
location = / {
# match/query.
}
location / {
# matches any query because all requests start with a /. But regular expression rules and long block rules will be preferentially matched against the query.
}
location ^~ /img/ {
# Match any query that starts with /img/ and stop searching. Any regular expressions will not be tested.
}
location ~*.(gif|jpg|jpeg|pbm|png)$ {
# matches any request that ends in gif,jpg, pbm,png or jpeg.
}