Nginx 位置正则表达式捕获变量

Rya*_*yan 5 regex nginx http-proxy

我有这个 REST API URL:

http://localhost:4000/api/v2/stocks/accounts/1162/tradings
Run Code Online (Sandbox Code Playgroud)

我希望它 proxy_pass 到 URL:

http://localhost:4001/api/v2/stocks/accounts/1162/tradings
Run Code Online (Sandbox Code Playgroud)

其中1162是URL参数,可以是其他值。

我有以下内容:

location ^~ /api/v2/stocks/accounts/([^/]+)/tradings {
      proxy_pass http://localhost:4001/api/v2/stocks/accounts/$1/tradings;
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用(404 Not Found),我用谷歌搜索了类似的问题,但没有太多帮助:

像这个:获取参数 Nginx 和 proxy_pass 的路径或这个:trouble with location regex and redirection in nginx

有什么办法可以使用 nginx 来实现我想要的吗?

预先感谢您的任何帮助。

添加:

我还添加了参数捕获:

location ~ /api/v2/stocks/accounts/([^/]+)/tradings/(.*) {
    proxy_pass http://localhost:4001/api/v2/stocks/accounts/$1/tradings/$2$is_args$args;
}
Run Code Online (Sandbox Code Playgroud)

zyw*_*ywj 5

你可以这样做。不是‘ ^~

location ~ /api/v2/stocks/accounts/([^/]+)/tradings {
  proxy_pass http://localhost:4001/api/v2/stocks/accounts/$1/tradings;
}
Run Code Online (Sandbox Code Playgroud)

正如nginx.org所描述的。
^ ~通常用于匹配目录,如下所示:

location ^~ /dir/ {
    # some actions
}
Run Code Online (Sandbox Code Playgroud)