- Purpose: Matches the beginning of a string (or URI).
- Example: Match URIs that start with
/blog/
.location ~ ^/blog/ { # Configuration }
- Purpose: Matches the end of a string.
- Example: Match URIs that end with
.html
.location ~ \.html$ { # Configuration }
-
Purpose: Matches any single character (except newlines).
-
Example: This would match any single character followed by php. For example, it could match
/xphp
,/9php
, or/@php
.location ~ .php$ { # Configuration }
- Purpose: Matches zero or more of any characters.
- Example: Match any URI starting with
/images/
followed by any characters.location ~ ^/images/.* { # Configuration }
- Purpose: Matches one or more of the preceding character or group.
- Example: Match URIs that contain one or more digits (like
/user/123
).location ~ /user/[0-9]+ { # Configuration }
- Purpose: Matches any single character within the brackets.
- Example: Match any URI that contains
/file1.html
or/file2.html
but not/file12.html
.location ~ /file[12]\.html$ { # Configuration }
- Purpose: Groups multiple characters into a single unit.
- Example: Capture any file extension after
/files/
.location ~ ^/files/(.*)\.(jpg|png|gif)$ { # Configuration }
- Purpose: Acts as an OR operator to match one or another option.
- Example: Match URIs that end with
.jpg
or.png
.location ~ \.(jpg|png)$ { # Configuration }
- Purpose: Matches any single digit (0-9).
- Example: Match any URI that contains a number.
location ~ /id/\d+ { # Configuration }
- Purpose: Matches any alphanumeric character or underscore.
- Example: Match any alphanumeric username in the URI.
location ~ ^/user/\w+$ { # Configuration }
-
Purpose: Makes the preceding character optional.
-
Example: Match URIs that start with
/blog/
or/blog
.location ~ ^/blog/? { # Configuration }
-
Purpose: Escapes special characters in regular expressions.
-
Example: The literal period (
.
) has been escaped.location ~ \.html$ { # Configuration }
- Purpose: Specifies a specific number of matches.
- Example: Match a URI that contains exactly three digits.
location ~ /product/[0-9]{3} { # Configuration }