From 868585dc9ad47996f3bdf641ecaa368e6c8dd4f4 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 25 May 2020 11:10:03 +0200 Subject: [PATCH] add custom error template and default error page location --- defaults/main.yml | 14 ++++ tasks/install.yml | 9 +++ templates/etc/nginx/nginx.conf.j2 | 2 +- templates/etc/nginx/sites-available/vhost.j2 | 17 ++++ templates/usr/error.html.j2 | 83 ++++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 templates/usr/error.html.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 51fba51..72a0b11 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -107,6 +107,19 @@ nginx_error_page: [] # dest: /4xx.html # @end +# @var nginx_error_location: description: > +# Default error location. If set, the defined location will be automatically added once +# to every server block to handle custom error sites. +# @end +# @var nginx_error_location:default: $ "_unset_" +# @var nginx_error_location: example: > +# nginx_error_location: +# match: / +# root: /var/www/vhosts/default +# index: index.html +# custom_options: +# @end + nginx_vhosts_dir: /var/www/vhosts nginx_vhosts_default: @@ -152,6 +165,7 @@ nginx_vhosts_default: # proxy_send_timeout: 3600s # proxy_headers: [] # proxy_intercept_errors: "off" +# custom_options: # custom_options: # - 'deny: all' diff --git a/tasks/install.yml b/tasks/install.yml index 9e719b1..c62a838 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -76,6 +76,15 @@ validate: /bin/bash -c 'nginx -t -c /dev/stdin <<< "events {worker_connections 10;} http { include %s; }"' notify: __nginx_reload + - name: Add custom error page template + template: + src: usr/share/nginx/html/error.html.j2 + dest: usr/share/nginx/html/error.html + owner: root + group: root + mode: 0644 + notify: __nginx_reload + - name: Set selinux booleans seboolean: name: "{{ item.name }}" diff --git a/templates/etc/nginx/nginx.conf.j2 b/templates/etc/nginx/nginx.conf.j2 index 5acc99a..c545574 100644 --- a/templates/etc/nginx/nginx.conf.j2 +++ b/templates/etc/nginx/nginx.conf.j2 @@ -31,7 +31,7 @@ http { {% if nginx_error_page is defined and nginx_error_page | length > 0 %} {% for error in nginx_error_page %} - error_page {{ error.code }} {{ error.dest }}; + error_page {{ error.code | join(" ") }} {{ error.dest }}; {% endfor %} {% endif %} diff --git a/templates/etc/nginx/sites-available/vhost.j2 b/templates/etc/nginx/sites-available/vhost.j2 index 95bd97e..6709e1b 100644 --- a/templates/etc/nginx/sites-available/vhost.j2 +++ b/templates/etc/nginx/sites-available/vhost.j2 @@ -90,5 +90,22 @@ server { {% endfor %} {% endif %} + {% if nginx_error_location is defined %} + location {{ nginx_error_location.match }} { + {% if nginx_error_location.root is defined and nginx_error_location.root %} + root {{ nginx_error_location.root }}; + {% endif %} + {% if nginx_error_location.index is defined and nginx_error_location.index %} + index {{ nginx_error_location.index }}; + + {% endif %} + {% if nginx_error_location.custom_options is defined and nginx_error_location.custom_options %} + + {% for inline_option in nginx_error_location.custom_options %} + {{ inline_option }}; + {% endfor %} + {% endif %} + } + {% endif %} } {% endfor %} diff --git a/templates/usr/error.html.j2 b/templates/usr/error.html.j2 new file mode 100644 index 0000000..95b66d6 --- /dev/null +++ b/templates/usr/error.html.j2 @@ -0,0 +1,83 @@ + + + + + + + + + Oops! You're lost. + + + + + +
+
+

+ +

+

Oops! You're lost.

+

+ +

+
+
+ +