微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有这个REST API URL:

http://localhost:4000/api/v2/stocks/accounts/1162/Tradings

我希望它到proxy_pass到URL:

http://localhost:4001/api/v2/stocks/accounts/1162/Tradings

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

我有以下内容

location ^~ /api/v2/stocks/accounts/([^/]+)/Tradings {
      proxy_pass http://localhost:4001/api/v2/stocks/accounts/$1/Tradings;
}

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

像这样:Get arguments Nginx and path for 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;
}

解决方法:

你可以这样做.不’^〜’

location ~ /api/v2/stocks/accounts/([^/]+)/Tradings {
  proxy_pass http://localhost:4001/api/v2/stocks/accounts/$1/Tradings;
}

nginx.org所述.
^〜经常匹配目录,如下所示:

location ^~ /dir/ {
    # some actions
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐