Skip to content

Commit d15e3e7

Browse files
committed
allow for multiple domains by using a name for upstream
also allows for regex domain matching, if desired you'd want to specify a static domain first so that it gets used for the upstream name, but the following server_names can be a regex pattern (probably in quotes, to be safe)
1 parent 4fffe33 commit d15e3e7

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
)
1919

2020
type container struct {
21+
Name string
2122
Host string
2223
Port string
2324
Address string
@@ -153,10 +154,10 @@ func writeConfig(params []*container) error {
153154
containers = make(map[string][]*container)
154155
// Remap the containers as a 2D array with the domain as the index
155156
for _, v := range params {
156-
if _, ok := containers[v.Host]; !ok {
157-
containers[v.Host] = make([]*container, 0)
157+
if _, ok := containers[v.Name]; !ok {
158+
containers[v.Name] = make([]*container, 0)
158159
}
159-
containers[v.Host] = append(containers[v.Host], v)
160+
containers[v.Name] = append(containers[v.Name], v)
160161
}
161162
tmpl, err := newTemplate(filepath.Base(*templateFile)).ParseFiles(*templateFile)
162163
if err != nil {

scanner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (s *scanner) extractContainer(t *ecs.Task, cd *ecs.ContainerDefinition) (*c
124124
return nil, errors.New("[" + *cd.Name + "] no valid port configuration found. skipping")
125125
}
126126
return &container{
127+
Name: strings.Fields(virtualHost)[0],
127128
Host: virtualHost,
128129
Port: port,
129130
Env: envVariables,

templates/nginx.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ server {
99
return 503;
1010
}
1111

12-
{{ range $domain, $container := . }}
13-
upstream {{ $domain }} {
12+
{{ range $name, $container := . }}
13+
upstream {{ $name }} {
1414
{{ range $_, $value := $container }}
1515
server {{ $value.Address }}:{{ $value.Port }};
1616
{{ end }}
1717
}
1818
server {
19-
server_name {{ $domain }};
19+
server_name {{ (index $container 0).Host }};
2020
listen 80;
2121
access_log /var/log/nginx/access.log vhost;
2222
location / {
23-
proxy_pass http://{{ $domain }};
23+
proxy_pass http://{{ $name }};
2424
}
2525
}
2626
{{ end }}

0 commit comments

Comments
 (0)