【发布时间】:2022-01-24 05:29:01
【问题描述】:
我正在尝试从请求 URL 中删除 /api/v1 并简单地传递其余部分。例如 /api/v1/test/ 就是 /test。 /api/v1/test/ready 和 /test/ready 一样简单。
这是我尝试过的。我在想第一个参数,即 $1 将在 ^(/api/v1) 中捕获,其余部分将被捕获 (.*?)。 所以,我只是通过 $2 并打破它。但是,它不起作用。我不确定我做错了什么。我尝试了一些调试,但没有这样做。
location /api/v1 {
rewrite ^(/api/v1)(.*?) $2; break;
# tried return 200 $2;
# but this will never hit since it is rewriting
# the rules will be evaluated again.
# I guess break will not allow it to re run again for the rules.
include uwsgi_params;
uwsgi_pass 10.0.2.15:3031;
}
我试图调试它,但我不能。 它从来没有命中位置/。例如。
location / {
return 200 $request_uri;
}
【问题讨论】:
-
字符串的其余部分不能用惰性模式匹配,将
(.*?)替换为(.*) -
另外,在
$2和break之间有一个虚假的;。