【发布时间】:2019-01-31 20:13:09
【问题描述】:
根据传递的参数,我有不同的服务将处理 URL 的不同部分
Service offer 将处理路由 /offer/44 和其他路由,如 /offer/44/calculator 以下代码成功处理了该问题。
location ^~ /offer {
location ^~ /offer {
rewrite ^/offer/(.*)$ /offer/$1 break;
proxy_pass http://localhost:4000/;
}
}
但是当我有 url /offer/44/payments/* 时,我的额外复杂层就来了,然后应该通过另一个服务路由
我尝试了很多不同的嵌套位置变体,但似乎对payments 没有任何作用。试图做类似以下的事情(重定向现在是基本的,因为我只是想让它只是重定向......但理想情况下我希望它在 payment 之后采取一切并将其放入其他服务 URL)
location ^~ /offer {
location ^~ /offer {
rewrite ^/offer/(.*)$ /offer/$1 break;
proxy_pass http://localhost:4000/;
}
#my ideal /offer/44/payments/*
location ^/offer/([0-9]+)/payment/(.*)$ {
proxy_pass http://payment-docker.local:20000/init;
}
}
所以我的问题是:我如何使本地嵌套工作,以便如果它在 offer 之后看到单词 payments 它将 proxy_pass 传递给另一个服务?
更新:
让它更简单
我尝试过的一些事情
看来nginx不喜欢的是数字
这行得通
# Example URL = http://localhost/offer/
location ^~ /offer/ {
proxy_pass http://payment-docker.local:20000/init;
}
这不起作用。只要有一个数字正则表达式,它总是返回404
# Example URL = http://localhost/offer/3
location ^~ /offer/([0-9]+) {
proxy_pass http://payment-docker.local:20000/init;
}
# Example URL = http://localhost/offer/33
location ^~ "/offer/[\d]{2}" {
proxy_pass http://payment-docker.local:20000/init;
}
【问题讨论】:
标签: nginx nginx-location nginx-reverse-proxy