🤖 Ai回答: In Nginx, the `~*` modifier is used in location blocks or rewrite rules to perform a case-insensitive regular expression match on the request URI、Here's a breakdown of its usage and significance:
1、What does `~*` mean?
`~`: Indicates a regular expression (regex) match.
`~*`: Adds case-insensitivity to the regex match (ignores uppercase/lowercase differences).
2、Syntax Example
nginx
location ~* regex_pattern { ..、}
Matches any URI that matches `regex_pattern`, regardless of letter case.
3、Key Differences from `~`
| Modifier | Behavior | Example |
| `~` | Case-sensitive regex match | `location ~ \.jpg$` matches only `.jpg` (not `.JPG`)、|
| `~*` | Case-insensitive regex match | `location ~* \.jpg$` matches `.jpg`, `.JPG`, `.Jpg`, etc、|
4、Common Use Cases
Static file handling: Match file extensions regardless of case (e.g., `.PNG`, `.png`).
nginx
location ~* \.(jpg|jpeg|png|gif)$ {
expires 1M;
add_header Cache-Control public;
}
5、Important Notes
Order matters: Nginx processes `location` blocks in a specific order:
1、Exact matches (`=`) first.
2、Then regex matches (`~`, `~*`).
3、Finally, prefix matches (`^~`).
Performance: Avoid overly broad regex patterns to prevent slow matching.
Use `~*` only when needed: Use `~` for case-sensitive matches (e.g., PHP scripts).
6、Example Configuration
nginx
server {
listen 80;
server_name 65.hk;
When to Use `~*`?
When you want to match URIs without caring about letter case (e.g., media files, SEO-friendly URLs).
When dealing with legacy systems that generate inconsistent case-sensitive URLs.
Avoiding Common Pitfalls
Overusing `~*`: It can slow down matching if applied to every location.
Conflicting locations: Ensure regex patterns don’t overlap ambiguously.
By understanding `~*`, you can effectively route traffic in Nginx while handling case variations gracefully.