]> git.somenet.org - root/pub/somesible.git/blob - roles/server/nginx/vhost-unified/templates/default/vhost.j2
roles/server/nginx/vhost-unified
[root/pub/somesible.git] / roles / server / nginx / vhost-unified / templates / default / vhost.j2
1 #
2 ################################################
3 ### Managed by someone's ansible provisioner ###
4 ################################################
5 # Part of: https://git.somenet.org/root/pub/somesible.git
6 # 2017-2026 by someone <someone@somenet.org>
7 #
8
9 {% if vhost_custom.vhost_custom_pre_server != "" %}
10
11 ###############################
12 ### vhost_custom_pre_server ###
13 ###############################
14 {{ vhost_custom.vhost_custom_pre_server }}
15
16 {% endif %}
17
18 {% if vhost_cache_on %}
19 proxy_cache_path /tmp/nginx_cachep_{{vhost_name}} levels=1:2 keys_zone=cachep_{{vhost_name}}:16m max_size=1g inactive=1440m use_temp_path=off;
20 fastcgi_cache_path /tmp/nginx_cachef_{{vhost_name}} levels=1:2 keys_zone=cachef_{{vhost_name}}:16m max_size=1g inactive=1440m use_temp_path=off;
21 {% endif %}
22
23 {% if vhost_https_on %}
24 server {
25     listen 443 ssl;
26     listen [::]:443 ssl;
27     server_name {{vhost_name}} {{vhost_aliases}} {{vhost_aliases_nocert}};
28     http2  on;
29
30     ssl_certificate /etc/ssl/letsencrypt/{{vhost_name}}/fullchain.pem;
31     ssl_certificate_key /etc/ssl/letsencrypt/{{vhost_name}}/privkey.pem;
32     ssl_certificate /etc/ssl/letsencrypt-rsa/{{vhost_name}}/fullchain.pem;
33     ssl_certificate_key /etc/ssl/letsencrypt-rsa/{{vhost_name}}/privkey.pem;
34     ssl_protocols TLSv1.2 TLSv1.3;
35     ssl_ciphers HIGH:!aNULL:!MD5:!SHA1:!SHA256:!SHA384;
36     ssl_prefer_server_ciphers on;
37 #    ssl_dhparam /etc/nginx/dhparams.pem;
38     ssl_session_cache shared:SSL:10m;
39     ssl_stapling on;
40     ssl_stapling_verify on;
41     add_header Strict-Transport-Security "max-age=31536000" always;
42
43     access_log /var/log/nginx/{{vhost_name}}-access.log;
44     error_log /var/log/nginx/{{vhost_name}}-error.log;
45
46     client_max_body_size 16G;
47     fastcgi_buffers 64 4K;
48     # fix 414 Request-URI Too Large.
49     large_client_header_buffers 4 64k;
50
51 {% if vhost_gzip_on %}
52     ### <gzip>
53     gzip on;
54     gzip_vary on;
55     gzip_proxied any;
56     gzip_comp_level 6;
57     gzip_buffers 16 8k;
58     gzip_http_version 1.1;
59     gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
60     ### </gzip>
61 {%- else %}
62     ### gzip DISABLED!
63 {%- endif %}
64
65 {% if vhost_cache_on %}
66     ### <cache>
67     proxy_cache cachep_{{vhost_name}};
68     proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
69     proxy_cache_background_update on;
70     proxy_cache_revalidate on;
71 #    proxy_cache_key $scheme$proxy_host$request_uri;
72 #    proxy_cache_valid 200 302 60m;
73 #    proxy_cache_valid 301     90m;
74 #    proxy_cache_valid any     10m;
75
76     fastcgi_cache cachef_{{vhost_name}};
77     fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503;
78     fastcgi_cache_background_update on;
79     fastcgi_cache_revalidate on;
80     fastcgi_cache_key $request_uri;
81 #    fastcgi_cache_valid 200 302 60m;
82 #    fastcgi_cache_valid 301     90m;
83 #    fastcgi_cache_valid any     10m;
84
85     add_header X-Cache-Status $upstream_cache_status always;
86     ### </cache>
87 {%- else %}
88     ### cache DISABLED!
89 {%- endif %}
90
91     ### <letsencrypt>
92     location ^~ /.well-known/acme-challenge {
93         alias /var/www/html/dehydrated;
94     }
95     ### </letsencrypt>
96 {%- if vhost_dotfile_protection %}
97
98     ### <dotfile protection>
99     location ~ /\.(?!well-known\/).* {
100         return 404;
101     }
102     ### </dotfile protection>
103 {%- else %}
104
105     ### dotfile protection DISABLED! (not vhost_dotfile_protection)
106 {%- endif %}
107
108     ### <robots.txt>
109     location = /robots.txt {
110         root /var/www/;
111         try_files /{{vhost_name}}/robots.txt /html/robots.txt =404;
112     }
113     ### </robots.txt>
114     ### <awstats>
115     location ^~ /awstats-icon {
116         alias /usr/share/awstats/icon/;
117         auth_pam "awstats";
118         auth_pam_service_name "nginx-awstats";
119         access_log off;
120     }
121     location = /awstats.pl {
122         root /usr/lib/cgi-bin/;
123         auth_pam "awstats";
124         auth_pam_service_name "nginx-awstats";
125         access_log off;
126
127         gzip off;
128         include fastcgi_params;
129         fastcgi_pass unix:/var/run/fcgiwrap.socket;
130         fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
131     }
132     ### </awstats>
133     ### <maintenance part1>
134     error_page 503 @maintenance;
135     location @maintenance {
136         default_type text/html;
137         root /var/www;
138         try_files /maintenance.html.{{vhost_name}} /maintenance.html /maintenance.html.dis =404;
139     }
140     set $maintenance "0";
141     if (-f "/var/www/maintenance.html") {
142        set $maintenance "1";
143     }
144     if (-f "/var/www/maintenance.html.{{vhost_name}}") {
145        set $maintenance "1";
146     }
147 {% for ip in vhost_maintenance_ips %}
148     if ($remote_addr = "{{ip}}") {
149         set $maintenance "0";
150     }
151 {% endfor %}
152     ### </maintenance part1>
153
154     ###############################
155     ### real config starts here ###
156     ###############################
157 {%- if vhost_type|lower() in ["php", "static"] %}
158
159     root /var/www/{{vhost_name}};
160     location / {
161         try_files $uri $uri/index.html $uri/ =404;
162     }
163
164     {%- if vhost_type|lower() == "static" %}
165
166     # remove /index.html from path
167     location ~ ^(.*/)index.html$ {
168         rewrite ^(.*/)index.html$ $1 permanent;
169     }
170
171     # remote trailing slashes from path
172     location ~ ^/(.*)/$ {
173         rewrite ^/(.*)/$ /$1 permanent;
174     }
175
176     {%- elif vhost_type|lower() == "php" %}
177
178     index index.php;
179
180     location ~ \.php($|/.*) {
181         if (!-f $document_root$fastcgi_script_name) {
182             return 404;
183         }
184
185         include fastcgi_params;
186         fastcgi_pass unix:/var/run/php/php-fpm.sock;
187         fastcgi_param SCRIPT_FILENAME $request_filename;
188
189         fastcgi_split_path_info ^(.+\.php)($|/.*);
190         fastcgi_param PATH_INFO $fastcgi_path_info;
191     }
192
193     {% endif %}
194 {%- elif vhost_type|lower() == "proxypass" %}
195
196     root /var/www/{{vhost_name}};
197     location / {
198         try_files $uri @proxy;
199     }
200     location @proxy {
201         proxy_pass {{vhost_proxypass_target}};
202         proxy_http_version 1.1;
203         proxy_set_header Upgrade $http_upgrade;
204         proxy_set_header Connection $connection_upgrade;
205         proxy_set_header Host $http_host;
206         proxy_set_header X-Real-IP $remote_addr;
207         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
208         proxy_set_header X-Forwarded-Proto $scheme;
209         proxy_set_header X-Frame-Options SAMEORIGIN;
210         proxy_set_header Accept-Encoding "";
211     }
212
213 {%- elif vhost_type|lower() == "redirect" %}
214
215     return {{vhost_redirect_code}} $scheme://{{vhost_redirect_target_without_protocol}}$request_uri;
216
217 {%- endif %}
218 {%- if vhost_custom.vhost_custom != "" %}
219
220     ####################
221     ### vhost_custom ###
222     ####################
223     {{ vhost_custom.vhost_custom | indent(width=4) }}
224
225 {%- endif %}
226
227     #############################
228     ### real config ends here ###
229     #############################
230     ### <maintenance part2>
231     if ($maintenance = "1") {
232         return 503;
233     }
234     ### </maintenance part2>
235 }
236
237 {% endif %}
238 {% if vhost_http_on %}
239 server {
240     listen 80;
241     listen [::]:80;
242     server_name {{vhost_name}} {{vhost_aliases}} {{vhost_aliases_nocert}};
243
244     access_log /var/log/nginx/{{vhost_name}}-access.log;
245     error_log /var/log/nginx/{{vhost_name}}-error.log;
246
247     client_max_body_size 1025M;
248     fastcgi_buffers 64 4K;
249     # fix 414 Request-URI Too Large.
250     large_client_header_buffers 4 64k;
251
252 {% if vhost_cache_on %}
253     ### <proxy_cache>
254     proxy_cache cachep_{{vhost_name}};
255     proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
256     proxy_cache_background_update on;
257     proxy_cache_revalidate on;
258 #    proxy_cache_key $scheme$proxy_host$request_uri;
259 #    proxy_cache_valid 200 302 60m;
260 #    proxy_cache_valid 301     90m;
261 #    proxy_cache_valid any     10m;
262
263     fastcgi_cache cachef_{{vhost_name}};
264     fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503;
265     fastcgi_cache_background_update on;
266     fastcgi_cache_revalidate on;
267     fastcgi_cache_key $request_uri;
268 #    fastcgi_cache_valid 200 302 60m;
269 #    fastcgi_cache_valid 301     90m;
270 #    fastcgi_cache_valid any     10m;
271
272     add_header X-Cache-Status $upstream_cache_status always;
273     ### </proxy_cache>
274 {%- else %}
275     ### proxy_cache DISABLED!
276 {%- endif %}
277
278     ### <letsencrypt>
279     location ^~ /.well-known/acme-challenge {
280         alias /var/www/html/dehydrated;
281     }
282     ### </letsencrypt>
283 {%- if vhost_dotfile_protection %}
284
285     ### <dotfile protection>
286     location ~ /\.(?!well-known\/).* {
287         return 404;
288     }
289     ### </dotfile protection>
290 {%- else %}
291
292     ### dotfile protection DISABLED! (not vhost_dotfile_protection)
293 {%- endif %}
294
295     ### <robots.txt>
296     location = /robots.txt {
297         root /var/www/;
298         try_files /{{vhost_name}}/$uri /html/$uri =404;
299     }
300     ### </robots.txt>
301     ### <awstats>
302     location ^~ /awstats-icon {
303         alias /usr/share/awstats/icon/;
304         auth_pam "awstats";
305         auth_pam_service_name "nginx-awstats";
306         access_log off;
307     }
308     location = /awstats.pl {
309         root /usr/lib/cgi-bin/;
310         auth_pam "awstats";
311         auth_pam_service_name "nginx-awstats";
312         access_log off;
313
314         gzip off;
315         include fastcgi_params;
316         fastcgi_pass unix:/var/run/fcgiwrap.socket;
317         fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
318     }
319     ### </awstats>
320     ### <maintenance part1>
321     error_page 503 @maintenance;
322     location @maintenance {
323         default_type text/html;
324         root /var/www;
325         try_files /maintenance.html.{{vhost_name}} /maintenance.html /maintenance.html.dis =404;
326     }
327     set $maintenance "0";
328     if (-f "/var/www/maintenance.html") {
329        set $maintenance "1";
330     }
331     if (-f "/var/www/maintenance.html.{{vhost_name}}") {
332        set $maintenance "1";
333     }
334 {% for ip in vhost_maintenance_ips %}
335     if ($remote_addr = "{{ip}}") {
336         set $maintenance "0";
337     }
338 {% endfor %}
339     ### </maintenance part1>
340
341     ###############################
342     ### real config starts here ###
343     ###############################
344 {%- if vhost_type|lower() in ["php", "static"] %}
345
346     root /var/www/{{vhost_name}};
347     location / {
348         try_files $uri $uri/index.html $uri/ =404;
349     }
350
351     {%- if vhost_type|lower() == "static" %}
352
353     # remove /index.html from path
354     location ~ ^(.*/)index.html$ {
355         rewrite ^(.*/)index.html$ $1 permanent;
356     }
357
358     # remote trailing slashes from path
359     location ~ ^/(.*)/$ {
360         rewrite ^/(.*)/$ /$1 permanent;
361     }
362
363     {%- elif vhost_type|lower() == "php" %}
364
365     index index.php;
366
367     location ~ \.php($|/.*) {
368         if (!-f $document_root$fastcgi_script_name) {
369             return 404;
370         }
371
372         include fastcgi_params;
373         fastcgi_pass unix:/var/run/php/php-fpm.sock;
374         fastcgi_param SCRIPT_FILENAME $request_filename;
375
376         fastcgi_split_path_info ^(.+\.php)($|/.*);
377         fastcgi_param PATH_INFO $fastcgi_path_info;
378     }
379
380     {% endif %}
381 {%- elif vhost_type|lower() == "proxypass" %}
382
383     root /var/www/{{vhost_name}};
384     location / {
385         try_files $uri @proxy;
386     }
387     location @proxy {
388         proxy_pass {{vhost_proxypass_target}};
389         proxy_http_version 1.1;
390         proxy_set_header Upgrade $http_upgrade;
391         proxy_set_header Connection $connection_upgrade;
392         proxy_set_header Host $http_host;
393         proxy_set_header X-Real-IP $remote_addr;
394         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
395         proxy_set_header X-Forwarded-Proto $scheme;
396         proxy_set_header X-Frame-Options SAMEORIGIN;
397         proxy_set_header Accept-Encoding "";
398     }
399
400 {%- elif vhost_type|lower() == "redirect" %}
401
402     return {{vhost_redirect_code}} $scheme://{{vhost_redirect_target_without_protocol}}$request_uri;
403
404 {%- endif %}
405 {%- if vhost_custom.vhost_custom != "" %}
406
407     ####################
408     ### vhost_custom ###
409     ####################
410     {{ vhost_custom.vhost_custom | indent(width=4) }}
411
412 {%- endif %}
413
414     #############################
415     ### real config ends here ###
416     #############################
417     ### <maintenance part2>
418     if ($maintenance = "1") {
419         return 503;
420     }
421     ### </maintenance part2>
422 }
423
424 {% endif %}