diff --git a/doc/site.md b/doc/site.md index 53a4be0..bc46507 100644 --- a/doc/site.md +++ b/doc/site.md @@ -19,6 +19,8 @@ Common - `redirect_https`: (O) Boolean. Redirect HTTP to HTTPS. If "true", you _MUST_ set `proto` to ```['https']```. - `location`: (O) Add new custom locations (it does not overwrite!) - `location_order`: (O) Due to non preditive `location` order, you can provide the good order (see test-location.local in [tests/test.yml](../tests/test.yml)). +- `location_before`: (O) Add new custom locations before generated location by template +- `location_order_before`: (O) Manages location order for `location_before` - `more`: (O) Add more custom infos. - `upstream_params`: (O) Add upstream params (useful when you want to pass variables to PHP) - `override_try_files`: (O) overrides default try\_files defined in template diff --git a/templates/etc/nginx/sites-available/_base.j2 b/templates/etc/nginx/sites-available/_base.j2 index 6cac863..0c7fe0e 100644 --- a/templates/etc/nginx/sites-available/_base.j2 +++ b/templates/etc/nginx/sites-available/_base.j2 @@ -5,9 +5,29 @@ {% set __http_proxy_protocol_port = item.http_proxy_protocol_port | default([]) %} {% set __https_proxy_protocol_port = item.https_proxy_protocol_port | default([]) %} {% set __location = item.location | default({}) %} +{% set __location_before = item.location_before | default({}) %} {% set __headers = item.headers | default(nginx_servers_default_headers) %} {% set __ssl_name = item.ssl_name | default(item.name if item.name is string else item.name[0]) %} {% set __location_order = item.location_order | default(__location.keys()) %} +{% set __location_order_before = item.location_order_before | default(__location_before.keys()) %} +{% macro locations(list, order) %} +{% if order | length > 0 %} + # --> Custom locations +{% for location in order %} + location {{ location }} { +{% set opts = list[location] %} +{% for opt in opts %} +{% if opt.htpasswd is defined %} +{{ htpasswd(opt.htpasswd, 2) }} +{% else %} + {{ opt }} +{% endif %} +{% endfor %} + } +{% endfor %} + # <-- Custom locations +{% endif %} +{% endmacro %} {% macro htpasswd(htpasswd_name, indent=1) -%} {%- if htpasswd_name != false %} {%- for ht in nginx_htpasswd if ht.name == htpasswd_name %} @@ -88,6 +108,8 @@ server { # <-- Custom headers {% endblock %} +{{ locations(__location_before, __location_order_before) }} + {% if not __location.has_key('/') %} location / { {% block template_try_files %} @@ -101,19 +123,7 @@ server { {% block template_custom_location %} {% endblock %} -{% if __location_order | length > 0 %} - # --> Custom locations -{% for location in __location_order %} - location {{ location }} { -{% set opts = __location[location] %} -{% for opt in opts %} -{% if opt.htpasswd is defined %}{{ htpasswd(opt.htpasswd, 2) }}{% else %} - {{ opt }} -{% endif %} -{% endfor %} - } -{% endfor %} # <-- Custom locations -{% endif %} +{{ locations(__location, __location_order) }} {% block template_local_content %} {% if item.manage_local_content is not defined or item.manage_local_content %} diff --git a/tests/test.yml b/tests/test.yml index bf500fd..b58f5df 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -164,9 +164,10 @@ - 'deny all;' - name: 'test-htpasswd.local' template: '_base' - location: + location_before: '/hello': - htpasswd: 'hello' + location: '/public': - htpasswd: false use_error_log: true @@ -175,20 +176,22 @@ htpasswd: 'hello' - name: 'test-location.local' template: '_base' + location_before: + '/b': + - 'alias /var/tmp;' + '/c': + - 'alias /var/tmp;' location: '/': - 'alias /var/tmp;' '/a': - 'alias /var/tmp;' - '/b': - - 'alias /var/tmp;' - '/c': - - 'alias /var/tmp;' + location_order_before: + - '/b' + - '/c' location_order: - '/' - '/a' - - '/b' - - '/c' - name: 'test-php.local' php_version: "{{ nginx_php.1.version if nginx_php.1 is defined else nginx_php.0.version }}" upstream_params: