From 90b6ddd1b38689ce15a13986eb6cd2fa607289e1 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Sat, 21 Feb 2015 00:02:51 -0600 Subject: [PATCH] Fixes #11: Add default/simple virtualhost configuration. --- README.md | 4 ++-- defaults/main.yml | 17 ++++++++++++++--- tasks/vhosts.yml | 4 ++-- templates/nginx.conf.j2 | 2 +- templates/vhosts.j2 | 25 ++++++++++++++++++++++++- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0626e9f..7f9074d 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ None. Available variables are listed below, along with default values (see `defaults/main.yml`): - nginx_vhost_path: /etc/nginx/sites-enabled + nginx_vhosts: [] -The path to the vhost configuration folder (where Nginx will look for server configurations). +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 `[]`. nginx_remove_default_vhost: false diff --git a/defaults/main.yml b/defaults/main.yml index 82ab704..41cf61a 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -4,8 +4,19 @@ nginx_worker_connections: "1024" nginx_client_max_body_size: "64m" nginx_keepalive_timeout: "65" +nginx_proxy_cache_path: "" + nginx_remove_default_vhost: false nginx_vhosts: [] -# TODO - add example. - -nginx_proxy_cache_path: "" +# Example vhost below, showing all available options: +# - { +# listen: "80 default_server", # default: "80 default_server" +# server_name: "example.com", # default: N/A +# root: "/var/www/example.com", # default: N/A +# index: "index.html index.htm", # default: "index.html index.htm" +# +# # Properties that are only added if defined: +# error_page: "", +# access_log: "", +# extra_config: "" # Can be used to add extra config blocks (multiline). +# } diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index a41401e..5ee8ec2 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -9,14 +9,14 @@ - name: Add managed vhost config file (if any vhosts are configured). template: src: vhosts.j2 - dest: "{{ nginx_vhost_path }}/vhosts" + dest: "{{ nginx_vhost_path }}/vhosts.conf" mode: 0644 when: nginx_vhosts notify: restart nginx - name: Remove managed vhost config file (if no vhosts are configured). file: - path: "{{ nginx_vhost_path }}/vhosts" + path: "{{ nginx_vhost_path }}/vhosts.conf" state: absent when: not nginx_vhosts notify: restart nginx diff --git a/templates/nginx.conf.j2 b/templates/nginx.conf.j2 index 51407c9..f1967d9 100644 --- a/templates/nginx.conf.j2 +++ b/templates/nginx.conf.j2 @@ -34,5 +34,5 @@ http { proxy_cache_path {{ nginx_proxy_cache_path }}; {% endif %} - include /etc/nginx/conf.d/*.conf; + include {{ nginx_vhost_path }}/*; } diff --git a/templates/vhosts.j2 b/templates/vhosts.j2 index 4640904..09bda35 100644 --- a/templates/vhosts.j2 +++ b/templates/vhosts.j2 @@ -1 +1,24 @@ -# TODO +{% for vhost in nginx_vhosts %} +server { + listen {{ vhost.listen | default('80 default_server') }}; + server_name {{ vhost.server_name }}; + + root {{ vhost.root }}; + index {{ vhost.index | default('index.html index.htm') }}; + + {% if vhost.error_page is defined %} + error_page {{ vhost.error_page }}; + {% endif %} + {% if vhost.access_log is defined %} + access_log {{ vhost.access_log }}; + {% endif %} + + {% if vhost.return is defined %} + return {{ vhost.return }}; + {% endif %} + + {% if vhost.extra_parameters is defined %} + {{ vhost.extra_parameters }}; + {% endif %} +} +{% endfor %}