Bläddra i källkod

Fixes #11: Add default/simple virtualhost configuration.

Jeff Geerling 10 år sedan
förälder
incheckning
90b6ddd1b3
5 ändrade filer med 43 tillägg och 9 borttagningar
  1. 2 2
      README.md
  2. 14 3
      defaults/main.yml
  3. 2 2
      tasks/vhosts.yml
  4. 1 1
      templates/nginx.conf.j2
  5. 24 1
      templates/vhosts.j2

+ 2 - 2
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
 

+ 14 - 3
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).
+# }

+ 2 - 2
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

+ 1 - 1
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 }}/*;
 }

+ 24 - 1
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 %}