我正在使用 confd 和 etcd。我正在关注 confd example for nginx .我将这些 key 放在我的 etcd 服务中:
curl http://127.0.0.1:4001/v2/keys/myapp/upstream -XPUT -d dir=true
curl http://127.0.0.1:4001/v2/keys/myapp/subdomain -XPUT -d value="myapp"
curl http://127.0.0.1:4001/v2/keys/myapp/upstream/app2 -XPUT -d value="10.0.1.101:80"
curl http://127.0.0.1:4001/v2/keys/myapp/upstream/app1 -XPUT -d value="10.0.1.100:80"
这是我的toml配置:
[template]
prefix = "myapp"
keys = [
"subdomain",
"upstream",
]
owner = "nginx"
mode = "0644"
src = "nginx.tmpl"
dest = "/tmp/myapp.conf"
check_cmd = "/usr/sbin/nginx -t -c {{ .src }}"
reload_cmd = "/usr/sbin/service nginx reload"
这是我的 tmpl 配置。
upstream {{.subdomain}} {
{{range $server := .upstream}}
#I want to get the key string value
server {{$sever.key}} {{$server.Value}};
{{end}}
}
server {
server_name {{.subdomain}}.example.com;
location / {
proxy_pass http://{{.subdomain}};
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
这里我想捕获像 app2 或 app1 这样的关键字符串值。我只知道如何获得它的值(value)。我想做这样的事情 {{$sever.key}} 但它不起作用。 {{$sever.key}} 上面的配置是错误的,但我这样做是为了显示我想要什么。
这个想法可行吗?
是否有任何保留词或语法?
最佳答案
我遇到了同样的问题。对我来说,这段代码有效(采用您的配置):
{{range gets "/myapp/upstream/*"}}
server {{base .Key}} {{.Value}}
{{end}}
对我来说,主要问题是 gets
函数。我在网上找到的所有示例都使用了 getvs
,它只提供值。 base
函数,只是删除 key 路径的第一部分。
有关更多信息,请查看 these confd docs .
关于service-discovery - 我如何使用 confd 获取使用 etcd 生成的 key 字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689834/