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