大网数据广告活动
服务器运维问答

What does location ^~ mean in an nginx location block

· 52CE 我爱测

I am looking at an nginx location block that has this symbol ^~ (caret followed by tilde) before the location block. What does it do? I am having a hard time googling for it. location ^~ /realestate/ { uwsgi_pass 127.0.0.1:2340; …

问题原文

原帖链接 · bernie2436 · CC BY-SA 4.0

I am looking at an nginx location block that has this symbol ^~ (caret followed by tilde) before the location block. What does it do? I am having a hard time googling for it.

location ^~ /realestate/ {
    uwsgi_pass 127.0.0.1:2340;
    include /etc/nginx/uwsgi_params;
}

社区高分解答

原帖链接 · Martin Melka · CC BY-SA 4.0

To add to Jon's answer and to correct Eric's misleading answer - the ^~ is NOT a regex match, it is a prefix match with a higher priority than regexes.

From the nginx documentation:

Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching).

So, only the locations with ~ and ~* modifier will be treated as regular expressions.

To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file.

This means that nginx first processes the non-regex locations, i.e.

  • location = /foo/bar (if this matches, processing ends immediately)
  • location /foo/bar
  • location ^~ /foo/bar

From these rules the longest match is found and remembered. Now nginx would move to evaluating all of the regex locations. But, the important part, written in the documentation as well:

If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.

This means that if the selected longest-prefix-location was location ^~ /foo/bar, the regex locations are skipped and the match returns.

If, however, the longest-prefix-location match is location /foo/bar, then regular expressions are evaluated and only if none applies, is the matched location used:

The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.

All of this put together, it's about the priority of matching:

1. `=`       Exact match, terminate immediately
2. `^~`      Longest-prefix-match (not regex!)
3. `~`, `~*` Regular expression case sensitive/insensitive
4. `/`       Longest-prefix-match

Numbers 2 and 4 use the exact same matching mechanism, the only difference is their priority against the regular expression matches.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-03-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。