如何在nginx中捕获路径的起点和路径的其余部分?

时间:2016-06-01 14:47:12

标签: nginx

我希望根据Nginx中的请求路径将“proxy_pass”发送到内部服务,我已经设法通过这样做:

location ~ ^/(.+)$ {
    resolver 127.0.0.1:53 ipv6=off;
    resolver_timeout  10s;
    # $1 is the regex match (i.e. the path)
    set $backend "http://$1-service/entry";
    proxy_pass $backend;
  }

这允许我反向代理内部服务,例如,如果我导航到http://my-nginx.com/some,它会将请求传递给http://some-service/entry

虽然这很好但我也希望能够将其余路径附加到内部服务。例如,如果我导航到http://my-nginx.com/some/x/y,我希望nginx将proxy_pass改为http://some-service/x/y(请注意,我想要导航到http://some-service/entry/x/y)。< / p>

我尝试了以下内容:

# I'd like to capture two groups, my intension is that when path 
# is "/some/x/y" then $1 should be "some" and $2 should be "/x/y"
location ~ ^/([A-Za-z0-9_-]+)(/[A-Za-z0-9_-]+)?$ {
    resolver 127.0.0.1:53 ipv6=off;
    resolver_timeout  10s;

    set $backend "http://$1-service/entry";

    if ($2) {
      set $backend "http://$1-service$2"
    }

    proxy_pass $backend;
}   

它只是给我以下错误:

2016/06/01 14:39:58 [error] 8#8: *2827 some could not be resolved (3: Host not found), client: 10.240.0.4, server: nginx, request: "GET /some/x/y HTTP/1.1", host: "my-host"

知道如何解决这个问题吗?

0 个答案:

没有答案