From 5b0977567c591da58ec4e891a10a986c45051676 Mon Sep 17 00:00:00 2001 From: Emilien Mantel Date: Wed, 23 Nov 2016 11:43:13 +0100 Subject: [PATCH] New feature: provide location order (#24 related) --- README.md | 2 +- doc/vhost.md | 3 ++- templates/etc/nginx/sites-available/_base.j2 | 6 ++++-- tests/test.yml | 11 +++++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f30f52f..17bff9d 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Features: - Fast PHP configuration - Preconfigured vhost templates (should work on many app) - Auto-configure HTTP2 on SSL/TLS vhosts -- Manage dynamic modules (install and loading) +- Manage dynamic modules (install and loading) Requirements ------------ diff --git a/doc/vhost.md b/doc/vhost.md index ef17e76..97830f7 100644 --- a/doc/vhost.md +++ b/doc/vhost.md @@ -18,6 +18,7 @@ Common - `redirect_to_code`: Redirect code (default: 302) - `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)). - `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 @@ -25,7 +26,7 @@ Common - `htpasswd`: (O) References name key in `nginx_htpasswd`. Enable auth basic on all vhost. - `proto`: (O) list of protocol used. Default is a list with "http". If you need http and https, you must set a list with "http" and "https". You can only set "https" without http support. - `ssl_name`: (D) name of the key used when using TLS/SSL. Optional when `proto` contains "https". If you don't set this value, it will search by `name`. -- `ssl_template` (O) "strong" (default) or "legacy". You can disable SSL helpers and add your own directives by setting "false". +- `ssl_template` (O) "strong" (default) or "legacy". You can disable SSL helpers and add your own directives by setting "false". - `php_version` (O) Sepecify PHP version (5 or 7) (O): Optional diff --git a/templates/etc/nginx/sites-available/_base.j2 b/templates/etc/nginx/sites-available/_base.j2 index df5c981..ccb251d 100644 --- a/templates/etc/nginx/sites-available/_base.j2 +++ b/templates/etc/nginx/sites-available/_base.j2 @@ -5,6 +5,7 @@ {% set __location = item.location | default({}) %} {% set __headers = item.headers | default({'X-Frame-Options': 'DENY always', 'X-Content-Type-Options': 'nosniff always' }) %} {% 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()) %} {% macro htpasswd(htpasswd_name, indent=1) -%} {% for ht in nginx_htpasswd if ht.name == htpasswd_name %} {{ "\t" * indent }}auth_basic "{{ ht.description }}"; @@ -84,10 +85,11 @@ server { {% block template_custom_location %} {% endblock %} -{% if __location is iterable and __location | length > 0 %} +{% if __location_order | length > 0 %} # --> Custom locations -{% for location, opts in __location.iteritems() %} +{% 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 }} diff --git a/tests/test.yml b/tests/test.yml index e2b755f..6aeb1c1 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -155,6 +155,17 @@ location: '/': - 'alias /var/tmp;' + '/a': + - 'alias /var/tmp;' + '/b': + - 'alias /var/tmp;' + '/c': + - 'alias /var/tmp;' + location_order: + - '/' + - '/a' + - '/b' + - '/c' - name: 'test-php.local' php_version: "{{ '7.0' if nginx_php70 else '5.6' }}" upstream_params: