Skip to content

Instantly share code, notes, and snippets.

@kelseyhightower
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save kelseyhightower/150d951eb1f5d9ac1858 to your computer and use it in GitHub Desktop.

Select an option

Save kelseyhightower/150d951eb1f5d9ac1858 to your computer and use it in GitHub Desktop.
# confdir
```
$ tree env_confd/
env_confd/
├── conf.d
│   └── nginx.toml
└── templates
└── nginx.tmpl
2 directories, 2 files
```
## nginx.toml
```
[template]
src = "nginx.tmpl"
dest = "/tmp/nginx.conf"
keys = [
"/nginx/services/a/name",
"/nginx/services/a/address",
"/nginx/services/a/shortname",
"/nginx/services/b/name",
"/nginx/services/b/address",
"/nginx/services/b/shortname",
]
```
## nginx.tmpl
```
{{range $service := ls "/nginx/services"}}
server {
{{$shortname := printf "/nginx/services/%s/shortname" $service}}
access_log /var/logs/nginx/{{getv $shortname}}/access.log;
error_log /var/logs/nginx/{{getv $shortname}}/error.log;
listen 443 ssl;
server_name {{getv (printf "/nginx/services/%s/name" $service)}}.local.io;
location / {
proxy_pass http://{{getv (printf "/nginx/services/%s/address" $service)}}.local.io$request_uri;
}
}
{{end}}
```
## Run confd
```
confd -onetime -confdir env_confd/ -backend env
```
```
cat /tmp/nginx.conf
server {
access_log /var/logs/nginx/a/access.log;
error_log /var/logs/nginx/a/error.log;
listen 443 ssl;
server_name ServiceA.local.io;
location / {
proxy_pass http://10.0.0.2.local.io$request_uri;
}
}
server {
access_log /var/logs/nginx/b/access.log;
error_log /var/logs/nginx/b/error.log;
listen 443 ssl;
server_name ServiceB.local.io;
location / {
proxy_pass http://10.0.0.3.local.io$request_uri;
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment