6161 {{ else if (exists " /etc/nginx/vhost.d/default_acl" ) }}
6262 include /etc/nginx/vhost.d /default_acl;
6363 {{ end }}
64- {{ else if (exists " /etc/nginx/htpasswd/default" ) }}
65- auth_basic " Restricted {{ .Host }}" ;
66- auth_basic_user_file /etc/nginx/htpasswd/default;
67- {{ if (exists (printf " /etc/nginx/vhost.d/%s _acl" .Host )) }}
68- include {{ printf " /etc/nginx/vhost.d/%s _acl" .Host }};
69- {{ else if (exists " /etc/nginx/vhost.d/default_acl" ) }}
70- include /etc/nginx/vhost.d /default_acl;
64+ {{/*
65+ Wildcard htpasswd support for WordPress Multisite.
66+ Naming convention: _wildcard.domain.com applies to domain.com AND *.domain.com
67+ Supports multi-level TLDs: _wildcard.domain.co.in works for domain.co.in AND *.domain.co.in
68+
69+ Lookup order (after exact match check on line 56):
70+ - For 4+ part domains: checks _wildcard.{last-3-parts}, then _wildcard.{last-2-parts}, then default
71+ - For 2-3 part domains: checks _wildcard.{last-2-parts}, then falls back to default
72+ - For single-part hostnames: uses default only
73+
74+ Note: Uses sprig's splitList and sub functions (available in docker-gen 0.7.4+)
75+ */ }}
76+ {{ else }}
77+ {{ $hostParts := splitList " ." .Host }}
78+ {{ $partsLen := len $hostParts }}
79+ {{/* For 4+ part domains, check last 3 parts first (e.g., _wildcard.domain.co.in for blog.domain.co.in) */ }}
80+ {{ if ge $partsLen 4 }}
81+ {{ $idx3 := sub $partsLen 3 }}
82+ {{ $idx2 := sub $partsLen 2 }}
83+ {{ $idx1 := sub $partsLen 1 }}
84+ {{ $baseDomain3 := printf " %s .%s .%s " (index $hostParts $idx3 ) (index $hostParts $idx2 ) (index $hostParts $idx1 ) }}
85+ {{ $wildcardHtpasswd3 := printf " /etc/nginx/htpasswd/_wildcard.%s " $baseDomain3 }}
86+ {{ if (exists $wildcardHtpasswd3 ) }}
87+ auth_basic " Restricted {{ .Host }}" ;
88+ auth_basic_user_file {{ ($wildcardHtpasswd3 ) }};
89+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _acl" .Host )) }}
90+ include {{ printf " /etc/nginx/vhost.d/%s _acl" .Host }};
91+ {{ else if (exists " /etc/nginx/vhost.d/default_acl" ) }}
92+ include /etc/nginx/vhost.d /default_acl;
93+ {{ end }}
94+ {{ else }}
95+ {{/* Fallback: check last 2 parts (e.g., _wildcard.co.in for blog.domain.co.in) */ }}
96+ {{ $baseDomain2 := printf " %s .%s " (index $hostParts $idx2 ) (index $hostParts $idx1 ) }}
97+ {{ $wildcardHtpasswd2 := printf " /etc/nginx/htpasswd/_wildcard.%s " $baseDomain2 }}
98+ {{ if (exists $wildcardHtpasswd2 ) }}
99+ auth_basic " Restricted {{ .Host }}" ;
100+ auth_basic_user_file {{ ($wildcardHtpasswd2 ) }};
101+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _acl" .Host )) }}
102+ include {{ printf " /etc/nginx/vhost.d/%s _acl" .Host }};
103+ {{ else if (exists " /etc/nginx/vhost.d/default_acl" ) }}
104+ include /etc/nginx/vhost.d /default_acl;
105+ {{ end }}
106+ {{ else if (exists " /etc/nginx/htpasswd/default" ) }}
107+ auth_basic " Restricted {{ .Host }}" ;
108+ auth_basic_user_file /etc/nginx/htpasswd/default;
109+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _acl" .Host )) }}
110+ include {{ printf " /etc/nginx/vhost.d/%s _acl" .Host }};
111+ {{ else if (exists " /etc/nginx/vhost.d/default_acl" ) }}
112+ include /etc/nginx/vhost.d /default_acl;
113+ {{ end }}
114+ {{ end }}
115+ {{ end }}
116+ {{ else if ge $partsLen 2 }}
117+ {{/* For 2-3 part domains, check last 2 parts (e.g., _wildcard.example.com for blog.example.com or example.com) */ }}
118+ {{ $idx2 := sub $partsLen 2 }}
119+ {{ $idx1 := sub $partsLen 1 }}
120+ {{ $baseDomain2 := printf " %s .%s " (index $hostParts $idx2 ) (index $hostParts $idx1 ) }}
121+ {{ $wildcardHtpasswd2 := printf " /etc/nginx/htpasswd/_wildcard.%s " $baseDomain2 }}
122+ {{ if (exists $wildcardHtpasswd2 ) }}
123+ auth_basic " Restricted {{ .Host }}" ;
124+ auth_basic_user_file {{ ($wildcardHtpasswd2 ) }};
125+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _acl" .Host )) }}
126+ include {{ printf " /etc/nginx/vhost.d/%s _acl" .Host }};
127+ {{ else if (exists " /etc/nginx/vhost.d/default_acl" ) }}
128+ include /etc/nginx/vhost.d /default_acl;
129+ {{ end }}
130+ {{ else if (exists " /etc/nginx/htpasswd/default" ) }}
131+ auth_basic " Restricted {{ .Host }}" ;
132+ auth_basic_user_file /etc/nginx/htpasswd/default;
133+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _acl" .Host )) }}
134+ include {{ printf " /etc/nginx/vhost.d/%s _acl" .Host }};
135+ {{ else if (exists " /etc/nginx/vhost.d/default_acl" ) }}
136+ include /etc/nginx/vhost.d /default_acl;
137+ {{ end }}
138+ {{ end }}
139+ {{ else if (exists " /etc/nginx/htpasswd/default" ) }}
140+ {{/* Single-part hostname - use default */ }}
141+ auth_basic " Restricted {{ .Host }}" ;
142+ auth_basic_user_file /etc/nginx/htpasswd/default;
143+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _acl" .Host )) }}
144+ include {{ printf " /etc/nginx/vhost.d/%s _acl" .Host }};
145+ {{ else if (exists " /etc/nginx/vhost.d/default_acl" ) }}
146+ include /etc/nginx/vhost.d /default_acl;
147+ {{ end }}
71148 {{ end }}
72149 {{ end }}
73150
@@ -146,8 +223,8 @@ map $scheme $proxy_x_forwarded_ssl {
146223gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
147224
148225log_format vhost '$host $remote_addr - $remote_user [$time_local ] '
149- '" $request" $status $body_bytes_sent '
150- '" $http_referer" " $http_user_agent" ';
226+ '" $request" $status $body_bytes_sent '
227+ '" $http_referer" " $http_user_agent" ';
151228
152229{{ if $ .Env.RESOLVERS }}
153230resolver {{ $ .Env.RESOLVERS }};
@@ -190,14 +267,14 @@ server {
190267 {{ end }}
191268
192269 root /etc/nginx/html ;
193-
270+
194271 # Custom error page for 503
195272 error_page 503 /default.html ;
196-
273+
197274 location / {
198275 return 503;
199276 }
200-
277+
201278 # Serve the error page without redirect
202279 location = /default.html {
203280 root /etc/nginx/html ;
@@ -219,14 +296,14 @@ server {
219296 {{ end }}
220297
221298 root /etc/nginx/html ;
222-
299+
223300 # Custom error page for 503
224301 error_page 503 /default.html ;
225-
302+
226303 location / {
227304 return 503;
228305 }
229-
306+
230307 # Serve the error page without redirect
231308 location = /default.html {
232309 root /etc/nginx/html ;
0 commit comments