better domain+location handling

This commit is contained in:
Mauro Torrez 2019-09-06 00:44:04 -03:00
parent 8bb5ff1cca
commit 0022ab678c
7 changed files with 61 additions and 4 deletions

View File

@ -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/

View File

@ -12,5 +12,5 @@ server {
server_name _;
root /usr/share/nginx/html;
include locations/*.conf;
include location/default/*.conf;
}

18
tasks/domain.yml Normal file
View File

@ -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

12
tasks/location.yml Normal file
View File

@ -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

View File

@ -13,7 +13,7 @@
- Dockerfile
- nginx.conf
- dhparams.pem
- default_ssl.conf
- default.conf
tags: skip_me
- name: Crear imagen my_nginx

24
templates/domain.conf.j2 Normal file
View File

@ -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 %}
}

View File

@ -0,0 +1,3 @@
location {{ ng_location_name }} {
{{ ng_location_content }}
}