From 0f6582f3aa807f77891d3da56ed6c9d7d3ee83d9 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 25 May 2020 10:15:42 +0200 Subject: [PATCH] add error_page related options --- defaults/main.yml | 15 ++++++++++++++- templates/etc/nginx/nginx.conf.j2 | 6 ++++++ templates/etc/nginx/sites-available/vhost.j2 | 12 +++++++----- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 89d48a3..00a24b0 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -98,6 +98,15 @@ nginx_maps: [] # - "/old/path /new_path" # @end +nginx_error_page: [] +# @var nginx_error_page:example: +# nginx_error_page: +# - code: +# - 404 +# - 403 +# dest: /4xx.html +# @end + nginx_vhosts_dir: /var/www/vhosts nginx_vhosts_default: @@ -109,6 +118,10 @@ nginx_vhosts_default: - match: / root: /var/www/vhosts/default index: index.html + - match: /50x.html + root: "/usr/share/nginx/html" + custom_options: + - "error_page 500 502 503 504 /50x.html" # @var nginx_vhosts_default:example: > # nginx_vhosts_default: @@ -141,7 +154,7 @@ nginx_vhosts_default: # proxy_intercept_errors: "off" # custom_options: # - 'deny: all' -# error_page: /usr/share/nginx/html + nginx_vhosts_extra: [] diff --git a/templates/etc/nginx/nginx.conf.j2 b/templates/etc/nginx/nginx.conf.j2 index d64079a..e0f6b90 100644 --- a/templates/etc/nginx/nginx.conf.j2 +++ b/templates/etc/nginx/nginx.conf.j2 @@ -28,7 +28,13 @@ http { {% else %} access_log off; {% endif %} + {% if nginx_error_page is defined and nginx_error_page | len > 0 %} + {% for error in nginx_error_page %} + error_page {{ error.code }} {{ error.dest }}; + {% endfor %} + + {% endif %} map $http_upgrade $connection_upgrade { default upgrade; '' close; diff --git a/templates/etc/nginx/sites-available/vhost.j2 b/templates/etc/nginx/sites-available/vhost.j2 index 202ee29..95bd97e 100644 --- a/templates/etc/nginx/sites-available/vhost.j2 +++ b/templates/etc/nginx/sites-available/vhost.j2 @@ -37,6 +37,13 @@ server { {% if server.send_timeout is defined and server.send_timeout %} send_timeout {{ server.send_timeout }}; {% endif %} + {% if server.custom_options is defined and server.custom_options %} + + {% for inline_option in server.custom_options %} + {{ inline_option }}; + {% endfor %} + {% endif %} + {% for location in server.locations %} location {{ location.match }} { {% if location.root is defined and location.root %} @@ -82,11 +89,6 @@ server { } {% endfor %} - # redirect server error pages to the static page /50x.html - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root {{ item.error_page | default("/usr/share/nginx/html") }}; - } {% endif %} } {% endfor %}