The exact logic for selecting a location to process a request is given below:
- Test the URI against all prefix strings.
- The “=” modifier defines an exact match of the URI and a prefix string. If the exact match is found, the search stops.
- If the “^~” modifier prepends the longest matching prefix string, the regular expressions are not checked.
- Store the longest matching prefix string.
- Start testing the URI against regular expressions.
- Break on the first matching regular expression and use the corresponding location.
- If no regular expression matches, use the location corresponding to the stored prefix string.
Nginx 配置中, location 后面的参数有两种形式: 前缀和正则。 前缀就是一个普通的字符串, 正则以 ~开头, 后面跟一个正则表达式。
1. 测试所有的前缀
2. “=” 修饰符代表精确匹配, 如果精确匹配到了, 搜索结束
3. “^~"修饰符修饰了最长匹配前缀, 正则表达式不再检查
4. 保存最长匹配前缀
5. 开始测试正则表达式
6. 一但匹配到一个正则表达式, 则使用此location
7. 如果没有匹配到正则, 则使用最长前缀