diff --git a/README.md b/README.md index 7f9074d..82fad2d 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,16 @@ Available variables are listed below, along with default values (see `defaults/m nginx_vhosts: [] -A list of vhost definitions (server blocks) for Nginx virtual hosts. If left empty, you will need to supply your own virtual host configuration. See the example in `defaults/main.yml` for available server options. If you have a large number of customizations required for your server definition(s), you're likely better off managing the vhost configuration file yourself, leaving this variable set to `[]`. +A list of vhost definitions (server blocks) for Nginx virtual hosts. If left empty, you will need to supply your own virtual host configuration. See the commented example in `defaults/main.yml` for available server options. If you have a large number of customizations required for your server definition(s), you're likely better off managing the vhost configuration file yourself, leaving this variable set to `[]`. nginx_remove_default_vhost: false Whether to remove the 'default' virtualhost configuration supplied by Nginx. Useful if you want the base `/` URL to be directed at one of your own virtual hosts configured in a separate .conf file. + nginx_upstreams: [] + +If you are configuring Nginx as a load balancer, you can define one or more upstream sets using this variable. In addition to defining at least one upstream, you would need to configure one of your server blocks to proxy requests through the defined upstream (e.g. `proxy_pass http://myapp1;`). See the commented example in `defaults/main.yml` for more information. + nginx_user: "nginx" The user under which Nginx will run. Defaults to `nginx` for RedHat, and `www-data` for Debian. diff --git a/defaults/main.yml b/defaults/main.yml index 41cf61a..d80a31a 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -20,3 +20,14 @@ nginx_vhosts: [] # access_log: "", # extra_config: "" # Can be used to add extra config blocks (multiline). # } + +nginx_upstreams: [] +# - { +# name: myapp1, +# strategy: "ip_hash", # "least_conn", etc. +# servers: { +# "srv1.example.com", +# "srv2.example.com weight=3", +# "srv3.example.com" +# } +# } diff --git a/templates/nginx.conf.j2 b/templates/nginx.conf.j2 index f1967d9..044cc2d 100644 --- a/templates/nginx.conf.j2 +++ b/templates/nginx.conf.j2 @@ -34,5 +34,16 @@ http { proxy_cache_path {{ nginx_proxy_cache_path }}; {% endif %} +{% for upstream in nginx_upstreams %} + upstream {{ upstream.name }} { +{% if upstream.strategy is defined %} + {{ upstream.strategy }}; +{% endif %} +{% for server in upstream.servers %} + server {{ server }}; +{% endfor %} + } +{% endfor %} + include {{ nginx_vhost_path }}/*; }