From 363e4f2289c9c7db4156a5adba1ca3d505757920 Mon Sep 17 00:00:00 2001 From: Esperat Julian Date: Fri, 6 Nov 2015 22:27:25 +0100 Subject: [PATCH] Created a "Location" template to remove duplicate code. I have no knowledge about this templating language, but I saw the "upstream" template reused at several places, so I think it should also work to share the part inside `location { ... }` which is responsible to allow per VirtualHost configuration. I hope this is useful. --- nginx.tmpl | 59 +++++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 255cc35..8c63683 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -15,6 +15,27 @@ {{ end }} {{ end }} +{{ define "location" }} + {{ if (exists (printf "/etc/nginx/vhost.d/%s" .Host)) }} + include {{ printf "/etc/nginx/vhost.d/%s" .Host }}; + {{ else if (exists "/etc/nginx/vhost.d/default") }} + include /etc/nginx/vhost.d/default; + {{ end }} + + location / { + proxy_pass {{ trim .Proto }}://{{ trim .Host }}; + {{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }} + auth_basic "Restricted {{ .Host }}"; + auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }}; + {{ end }} + {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }} + include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}}; + {{ else if (exists "/etc/nginx/vhost.d/default_location") }} + include /etc/nginx/vhost.d/default_location; + {{ end }} + } +{{ end }} + # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the # scheme used to connect to this server map $http_x_forwarded_proto $proxy_x_forwarded_proto { @@ -137,24 +158,7 @@ server { add_header Strict-Transport-Security "max-age=31536000"; - {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }} - include {{ printf "/etc/nginx/vhost.d/%s" $host }}; - {{ else if (exists "/etc/nginx/vhost.d/default") }} - include /etc/nginx/vhost.d/default; - {{ end }} - - location / { - proxy_pass {{ trim $proto }}://{{ trim $host }}; - {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }} - auth_basic "Restricted {{ $host }}"; - auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }}; - {{ end }} - {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }} - include {{ printf "/etc/nginx/vhost.d/%s_location" $host}}; - {{ else if (exists "/etc/nginx/vhost.d/default_location") }} - include /etc/nginx/vhost.d/default_location; - {{ end }} - } + {{ template "location" (dict "Host" $host "Proto" $proto) }} } {{ else }} @@ -163,24 +167,7 @@ server { listen 80 {{ $default_server }}; access_log /var/log/nginx/access.log vhost; - {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }} - include {{ printf "/etc/nginx/vhost.d/%s" $host }}; - {{ else if (exists "/etc/nginx/vhost.d/default") }} - include /etc/nginx/vhost.d/default; - {{ end }} - - location / { - proxy_pass {{ trim $proto }}://{{ trim $host }}; - {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }} - auth_basic "Restricted {{ $host }}"; - auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }}; - {{ end }} - {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }} - include {{ printf "/etc/nginx/vhost.d/%s_location" $host}}; - {{ else if (exists "/etc/nginx/vhost.d/default_location") }} - include /etc/nginx/vhost.d/default_location; - {{ end }} - } + {{ template "location" (dict "Host" $host "Proto" $proto) }} } {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}