在nginx.conf location中,^和$的作用是什么?

location ~ ^/test/(?<id>\d+)$ {
    postgres_pass    database;
    rds_json         on;
    postgres_escape  $name $id;
    postgres_query   "SELECT $name";
}

上面的代码片段取自于https://github.com/FRiCKLE/ngx_postgres/issues/4

有人能解释一下 ^ 和 $ 的作用吗? 我找不到解释它们含义的文档。 已经查阅了https://github.com/FRiCKLE/ngx_postgreshttp://nginx.org/en/docs/http/ngx_http_core_module.html#location

点赞
用户2057919
用户2057919

这是一个正则表达式。请谷歌正则表达式。

^匹配字符串开头,$匹配字符串结尾。换句话说,这个正则表达式只会匹配location的值完全符合^$之间的部分。因此,只有以/test/开头,后跟一个或多个数字并且在字符串末尾的URL才能匹配。

2015-09-28 04:17:27