From 0022ab678ca3a3da335f3d9355186987c15ed424 Mon Sep 17 00:00:00 2001 From: Mauro Torrez Date: Fri, 6 Sep 2019 00:44:04 -0300 Subject: [PATCH] better domain+location handling --- files/Dockerfile | 4 ++-- files/{default_ssl.conf => default.conf} | 2 +- tasks/domain.yml | 18 ++++++++++++++++++ tasks/location.yml | 12 ++++++++++++ tasks/main.yml | 2 +- templates/domain.conf.j2 | 24 ++++++++++++++++++++++++ templates/location.conf.j2 | 3 +++ 7 files changed, 61 insertions(+), 4 deletions(-) rename files/{default_ssl.conf => default.conf} (89%) create mode 100644 tasks/domain.yml create mode 100644 tasks/location.yml create mode 100644 templates/domain.conf.j2 create mode 100644 templates/location.conf.j2 diff --git a/files/Dockerfile b/files/Dockerfile index 2d9edea..6b045d6 100644 --- a/files/Dockerfile +++ b/files/Dockerfile @@ -12,9 +12,9 @@ RUN apt-get update \ ADD nginx.conf /etc/nginx/ ADD dhparams.pem /etc/nginx/ssl/ -ADD default_ssl.conf /etc/nginx/conf.d/ +ADD default.conf /etc/nginx/conf.d/ VOLUME /etc/nginx/ssl/ VOLUME /etc/nginx/conf.d/ -VOLUME /etc/nginx/locations/ +VOLUME /etc/nginx/location/ VOLUME /usr/share/nginx/html/ diff --git a/files/default_ssl.conf b/files/default.conf similarity index 89% rename from files/default_ssl.conf rename to files/default.conf index 78e86f1..db23959 100644 --- a/files/default_ssl.conf +++ b/files/default.conf @@ -12,5 +12,5 @@ server { server_name _; root /usr/share/nginx/html; - include locations/*.conf; + include location/default/*.conf; } diff --git a/tasks/domain.yml b/tasks/domain.yml new file mode 100644 index 0000000..870885e --- /dev/null +++ b/tasks/domain.yml @@ -0,0 +1,18 @@ +--- +# template domain config +# vars: ng_domain_secure (True) +# ng_domain_flags (['http2']) +# ng_domain_name (no default) +# ng_domain_root ('/usr/share/nginx/html') +# ng_domain_common_locations (True) + +- name: domain locations directory + file: + path: "{{ nginx_location_mountpoint }}/{{ ng_domain_name }}" + state: directory + +- name: template domain config + template: + src: domain.conf.j2 + dest: "{{ nginx_config_mountpoint }}/{{ ng_domain_name }}.conf" + notify: restart nginx container diff --git a/tasks/location.yml b/tasks/location.yml new file mode 100644 index 0000000..7450b60 --- /dev/null +++ b/tasks/location.yml @@ -0,0 +1,12 @@ +--- +# template domain config +# ng_domain_name (no default) +# ng_location_name (no default) +# ng_location_content (no default) + +- name: template location config + template: + src: location.conf.j2 + dest: "{{ nginx_location_mountpoint }}/{{ ng_domain_name }}/{{ + ng_location_name|regex_replace('[^0-9a-zA-Z_-.]','_') }}.conf" + notify: restart nginx container diff --git a/tasks/main.yml b/tasks/main.yml index 75a30ca..4264670 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -13,7 +13,7 @@ - Dockerfile - nginx.conf - dhparams.pem - - default_ssl.conf + - default.conf tags: skip_me - name: Crear imagen my_nginx diff --git a/templates/domain.conf.j2 b/templates/domain.conf.j2 new file mode 100644 index 0000000..70f49d4 --- /dev/null +++ b/templates/domain.conf.j2 @@ -0,0 +1,24 @@ +{% if ng_domain_secure|default(True) %} +server { + listen 80; + listen [::]:80; + # redirect all HTTP requests to HTTPS with a 301 Moved Permanently response. + return 301 https://$host$request_uri; +} +{% endif %} + +server { + {% if not ng_domain_secure %} + listen 80 {{ ng_domain_flags | default(['http2']) | join(' ') }}; + listen [::]:80 {{ ng_domain_flags | default(['http2']) | join(' ') }}; + {% endif %} + listen 443 ssl {{ ng_domain_flags | default(['http2']) | join(' ') }}; + listen [::]:443 ssl {{ ng_domain_flags | default(['http2']) | join(' ') }}; + + server_name {{ ng_domain_name | regex_replace('^default$','_') }}; + root {{ ng_domain_root|default('/usr/share/nginx/html') }}; + include locations/{{ ng_domain_name }}/*.conf; + {% if ng_domain_common_locations | default(True) %} + include locations/common/*.conf; + {% endif %} +} diff --git a/templates/location.conf.j2 b/templates/location.conf.j2 new file mode 100644 index 0000000..e527ed6 --- /dev/null +++ b/templates/location.conf.j2 @@ -0,0 +1,3 @@ +location {{ ng_location_name }} { + {{ ng_location_content }} +}