Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit01dfb5b

Browse files
committed
nginx.tmpl: use closest vhost.d and htpasswd files (#1309)
This changes the logic for finding a matching file to the same one usedby certificates. This allows wildcard host names to find a matchingconfig file.The container prints "Template error: open /etc/nginx/htpasswd: no suchfile or directory" if /etc/nginx/htpasswd doesn't exist, but thisdoesn't impact functionality. A fix would be to make sure that directoryis always created.
1 parentc8a6785 commit01dfb5b

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

‎nginx.tmpl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,18 @@ upstream {{ $upstream_name }} {
241241

242242
{{ $is_https := (and (ne $https_method "nohttps") (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
243243

244+
{{/* Get the best matching vhost.d include file */}}
245+
{{ $vhostFile := closest (dir "/etc/nginx/vhost.d") ($host) }}
246+
{{ $vhostFile := when (ne $vhostFile "") (print "/etc/nginx/vhost.d/" $vhostFile) "" }}
247+
248+
{{/* Get the best matching vhost.d location include file */}}
249+
{{ $vhostLocationFile := closest (dir "/etc/nginx/vhost.d") (print $host "_location") }}
250+
{{ $vhostLocationFile := when (ne $vhostLocationFile "") (print "/etc/nginx/vhost.d/" $vhostLocationFile) "" }}
251+
252+
{{/* Get the best matching htpasswd file */}}
253+
{{ $htpasswdFile := closest (dir "/etc/nginx/htpasswd") $host }}
254+
{{ $htpasswdFile := when (ne $htpasswdFile "") (print "/etc/nginx/htpasswd/" $htpasswdFile) "" }}
255+
244256
{{ if $is_https }}
245257

246258
{{ if eq $https_method "redirect" }}
@@ -303,8 +315,8 @@ server {
303315
add_header Strict-Transport-Security "{{ trim $hsts }}" always;
304316
{{ end }}
305317

306-
{{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
307-
include {{printf "/etc/nginx/vhost.d/%s" $host }};
318+
{{ if (and (not (hasSuffix "_location" $vhostFile)) (exists $vhostFile)) }}
319+
include {{$vhostFile }};
308320
{{ else if (exists "/etc/nginx/vhost.d/default") }}
309321
include /etc/nginx/vhost.d/default;
310322
{{ end }}
@@ -323,12 +335,12 @@ server {
323335
proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
324336
{{ end }}
325337

326-
{{ if(exists(printf "/etc/nginx/htpasswd/%s" $host)) }}
338+
{{ if exists$htpasswdFile }}
327339
auth_basic"Restricted {{ $host }}";
328-
auth_basic_user_file{{(printf "/etc/nginx/htpasswd/%s" $host) }};
340+
auth_basic_user_file{{$htpasswdFile }};
329341
{{ end }}
330-
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
331-
include {{printf "/etc/nginx/vhost.d/%s_location" $host}};
342+
{{ if (and (hasSuffix "_location" $vhostLocationFile) (exists $vhostLocationFile)) }}
343+
include {{$vhostLocationFile}};
332344
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
333345
include /etc/nginx/vhost.d/default_location;
334346
{{ end }}
@@ -352,8 +364,8 @@ server {
352364
include /etc/nginx/network_internal.conf;
353365
{{ end }}
354366

355-
{{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
356-
include {{printf "/etc/nginx/vhost.d/%s" $host }};
367+
{{ if (and (not (hasSuffix "_location" $vhostFile)) (exists $vhostFile)) }}
368+
include {{$vhostFile }};
357369
{{ else if (exists "/etc/nginx/vhost.d/default") }}
358370
include /etc/nginx/vhost.d/default;
359371
{{ end }}
@@ -371,12 +383,13 @@ server {
371383
{{ else }}
372384
proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
373385
{{ end }}
374-
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
386+
387+
{{ if exists $htpasswdFile }}
375388
auth_basic"Restricted {{ $host }}";
376-
auth_basic_user_file{{(printf "/etc/nginx/htpasswd/%s" $host) }};
389+
auth_basic_user_file{{$htpasswdFile }};
377390
{{ end }}
378-
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
379-
include {{printf "/etc/nginx/vhost.d/%s_location" $host}};
391+
{{ if (and (hasSuffix "_location" $vhostLocationFile) (exists $vhostLocationFile)) }}
392+
include {{$vhostLocationFile}};
380393
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
381394
include /etc/nginx/vhost.d/default_location;
382395
{{ end }}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp