From 80e3cae22ed350b23a1fe8a417a2d2c34a5097ad Mon Sep 17 00:00:00 2001 From: Emilien Mantel Date: Tue, 1 Dec 2015 15:46:57 +0100 Subject: [PATCH] New feature on vhosts: "redirect_to" --- README.md | 8 +++++--- tasks/vhost.yml | 8 +++++--- templates/etc/nginx/sites-available/_base.j2 | 6 ++++-- templates/etc/nginx/sites-available/_redirect.j2 | 14 ++++++++++++++ tests/test.yml | 2 ++ 5 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 templates/etc/nginx/sites-available/_redirect.j2 diff --git a/README.md b/README.md index d92b7a0..654b0b8 100644 --- a/README.md +++ b/README.md @@ -55,18 +55,20 @@ You can see many examples in: [tests/test.yml](tests/test.yml). #### Common - `name`: (M) Domain or list of domain used. - - `template`: (M) template used to create vhost. Optional if you set `delete` to true. + - `template`: (D) template used to create vhost. Optional if you set `delete` to true or using `redirect_tor`. - `enable`: (O) Enable the vhost (default is true) - `delete`: (O) Delete the vhost (default is false) - `redirect_from`: (O) Domain list to redirect to the first `name`. You can use this key to redirect non-www to www + - `redirect_to`: (O) Redirect (302) all requests to this domain. Please set scheme (http:// or https:// or $sheme). - `location`: (O) Add new custom locations (it does not overwrite!) - `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 - `manage_local_content`: (O) Boolean. Set to false if you don't want to manage local content (images, css...). This option is useless if you use proxy `template` -(O) : Optional -(M) : Mandatory +(O): Optional +(M): Mandatory +(D): Depends other keys... #### Templates diff --git a/tasks/vhost.yml b/tasks/vhost.yml index 3102000..89864b0 100644 --- a/tasks/vhost.yml +++ b/tasks/vhost.yml @@ -16,7 +16,8 @@ when: > item.root is not defined and (item.template is defined and item.template != '_proxy') and - (item.delete is not defined or not item.delete) + (item.delete is not defined or not item.delete) and + item.redirect_to is not defined - name: FILE | Create root public folders (foreach nginx_vhosts) file: > @@ -29,11 +30,12 @@ when: > item.root is not defined and (item.template is defined and item.template != '_proxy') and - (item.delete is not defined or not item.delete) + (item.delete is not defined or not item.delete) and + item.redirect_to is not defined - name: TEMPLATE | Create vhosts template: > - src=etc/nginx/sites-available/{{ item.template }}.j2 + src=etc/nginx/sites-available/{{ item.template if item.redirect_to is not defined else '_redirect' }}.j2 dest=/etc/nginx/sites-available/{{ item.name if item.name is string else item.name[0] }} with_items: nginx_vhosts notify: reload nginx diff --git a/templates/etc/nginx/sites-available/_base.j2 b/templates/etc/nginx/sites-available/_base.j2 index 317f0de..50e33dd 100644 --- a/templates/etc/nginx/sites-available/_base.j2 +++ b/templates/etc/nginx/sites-available/_base.j2 @@ -13,11 +13,13 @@ server { listen {{ port }}; {% endfor %} server_name {% if item.name is string %}{{ item.name }}{% else %}{{ item.name | join(' ') }}{% endif %}; +{% block root %} {% if item.root is defined %} root {{ item.root }}; {% else %} root {{ nginx_root }}/{{ item.name if item.name is string else item.name[0] }}/public; {% endif %} +{% endblock %} {% block template_index %} index {{ item.index | default('index.html index.htm') }}; {% endblock %} @@ -41,12 +43,12 @@ server { {% block template_custom_location %} {% endblock %} +{% block template_local_content %} +{% if item.manage_local_content is not defined or item.manage_local_content %} location ~ /\.ht { deny all; } -{% block template_local_content %} -{% if item.manage_local_content is not defined or item.manage_local_content %} location = /favicon.ico { expires 30d; access_log off; diff --git a/templates/etc/nginx/sites-available/_redirect.j2 b/templates/etc/nginx/sites-available/_redirect.j2 new file mode 100644 index 0000000..bc6ad5c --- /dev/null +++ b/templates/etc/nginx/sites-available/_redirect.j2 @@ -0,0 +1,14 @@ +{% extends "_base.j2" %} + +{% block root %} +{% endblock %} + +{% block template_index %} +{% endblock %} + +{% block template_try_files %} + return 302 {{ item.redirect_to }}$request_uri; +{% endblock %} + +{% block template_local_content %} +{% endblock %} diff --git a/tests/test.yml b/tests/test.yml index 6afe39e..ed0a90d 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -50,6 +50,8 @@ upstream_name: 'test' - name: 'deleted.local' delete: true + - name: 'redirect-to.local' + redirect_to: 'http://test.local' roles: - ../../ post_tasks: