【发布时间】:2016-08-25 11:24:04
【问题描述】:
我有一个在 nginx 后面运行的应用程序。为了允许客户在相同的主机名下添加他们自己的工具,我们使用 location 类似这样:
location /some-extension {
rewrite ^/(.*) /$1 break;
proxy_pass http://customers-app/$1/$args;
}
现在,我想让这个动态化,以便给定的客户可以创建零个或多个这样的位置。由于应用程序是使用 Docker 部署的,因此我无法手动编辑 nginx 配置。
Nginx 是用 perl 和 lua 支持编译的,所以我在想这样的事情:
- 使用
path1 url1 path-2 url2 ... pathn urln形式的环境变量来配置外部工具。 - 在特殊的
location配置中,将请求URL 的第一个路径段与环境变量和proxy_pass匹配到相应的URL(如果找到)。
到目前为止,这就是我所拥有的:
location / {
content_by_lua '
local target_path = ngx.var.uri;
local name = nil
if target_path:len() > 0 then
startAt = target_path:find("/") + 1
endAt = target_path:find("/", startAt) - 1
name = target_path:sub(startAt,endAt)
ngx.say(name)
end
if name then
local custom_proxies = os.getenv("CUSTOM_PROXIES");
inString = custom_proxies:find("another ")
if not inString then
ngx.say("not in string")
else
startAt = custom_proxies:find(" ", inString + 1) + 1
endAt = custom_proxies:find(" ", startAt)
url = custom_proxies:sub(startAt,endAt)
ngx.say(url)
end
end
';
}
我知道我不应该使用content_by_lua,但它似乎有点用。问题是我怎样才能得到这个到proxy_pass到指定的URL?
【问题讨论】: