Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow upstreams to have multiple servers for load balancing #3

Merged
merged 1 commit into from Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ func runSignal() error {
}

func writeConfig(params []*container) error {
var containers map[string][]*container
containers = make(map[string][]*container);
// Remap the containers as a 2D array with the domain as the index
for _, v := range params {
if _, ok := containers[v.Host]; !ok {
containers[v.Host] = make([]*container, 0)
}
containers[v.Host] = append(containers[v.Host], v)
}
tmpl, err := template.ParseFiles(*templateFile)
if err != nil {
return err
Expand All @@ -115,7 +124,7 @@ func writeConfig(params []*container) error {
return err
}
defer f.Close()
return tmpl.Execute(f, params)
return tmpl.Execute(f, containers)
}

func findClusterName() (*string, error) {
Expand Down
10 changes: 6 additions & 4 deletions templates/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ server {
return 503;
}

{{ range $index, $value := . }}
upstream {{ $value.Host }} {
{{ range $domain, $container := . }}
upstream {{ $domain }} {
{{ range $_, $value := $container }}
server {{ $value.Address }}:{{ $value.Port }};
{{ end }}
}
server {
server_name {{ $value.Host }};
server_name {{ $domain }};
listen 80;
access_log /var/log/nginx/access.log vhost;
location / {
proxy_pass http://{{ $value.Host }};
proxy_pass http://{{ $domain }};
}
}
{{ end }}