Skip to content

Commit 5beece3

Browse files
committed
Issue geerlingguy#1254: Allow nginx vhost template to be overridden through template inheritance
1 parent c73c462 commit 5beece3

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

default.config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ nginx_hosts:
165165

166166
nginx_remove_default_vhost: true
167167
nginx_ppa_use: true
168+
nginx_vhost_template: "templates/nginx-vhost.conf.j2"
168169

169170
# MySQL databases and users.
170171
mysql_databases:

docs/configurations/webservers-nginx.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,43 @@ If you are using Ubuntu as your base OS and you want to get started quickly with
3939
ssl_protocols TLSv1.1 TLSv1.2;
4040
ssl_ciphers HIGH:!aNULL:!MD5;
4141
```
42+
43+
### Customizing server block configuration
44+
45+
If you can't customize via variables because an option isn't exposed, you can override the template used to generate the the virtualhost configuration file.
46+
47+
```yaml
48+
nginx_vhost_template: "{{ config_dir }}/nginx-vhost.conf.j2"
49+
```
50+
51+
You can either copy and modify the provided `nginx-vhost.conf.j2` template, or extend it and and override the specific template block you need to change.
52+
53+
_If you extend Drupal VM's provided base template, the path referenced should to be relative to the playbook.yml._
54+
55+
```
56+
{% extends 'templates/nginx-vhost.conf.j2' %}
57+
58+
{% block location_primary %}
59+
location / {
60+
try_files $uri @rewrite; # For Drupal <= 6
61+
}
62+
{% endblock %}
63+
64+
{% block location_image_styles %}
65+
location ~ ^/sites/.*/files/imagecache/ {
66+
try_files $uri @rewrite; # For Drupal <= 6
67+
}
68+
{% endblock %}
69+
```
70+
71+
If you need to append or prepend content to a block, you can use the `{{ super() }}` Jinja2 function to return the original block content from the base template.
72+
73+
```
74+
{% block location_deny %}
75+
{{ super() }}
76+
location ~* \.(txt|log)$ {
77+
allow 192.168.0.0/16;
78+
deny all;
79+
}
80+
{% endblock %}
81+
```

provisioning/roles/drupalvm.www/tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
- name: Copy Nginx vhosts into place.
9090
template:
91-
src: ../templates/nginx-vhost.conf.j2
91+
src: "{{ nginx_vhost_template }}"
9292
dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
9393
force: yes
9494
owner: root

provisioning/templates/nginx-vhost.conf.j2

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
1+
{% block redirect %}
12
{% if item.server_name_redirect is defined %}
23
server {
34
listen 80;
45
server_name {{ item.server_name_redirect }};
56
return 301 http://{{ item.server_name.split(' ')[0] }}$request_uri;
67
}
78
{% endif %}
9+
{% endblock %}
810

911
server {
12+
{% block server_id -%}
1013
listen {{ item.listen | default("80") }};
11-
1214
server_name {{ item.server_name }};
15+
{% endblock %}
16+
17+
{% block server_logs -%}
1318
error_log /var/log/nginx/error.log info;
19+
{% endblock %}
20+
21+
{% block server_basics -%}
1422
root {{ item.root }};
1523
index index.php index.html index.htm;
24+
{% endblock %}
1625

1726
{% if item.is_php is defined and item.is_php %}
27+
{% block location_primary -%}
1828
location / {
1929
# Don't touch PHP for static content.
2030
try_files $uri $uri/ /index.php?$query_string;
2131
}
32+
{% endblock %}
2233

34+
{% block location_deny -%}
2335
# Don't allow direct access to PHP files in the vendor directory.
2436
location ~ /vendor/.*\.php$ {
2537
deny all;
2638
return 404;
2739
}
40+
location ~ (^|/)\. {
41+
return 403;
42+
}
43+
{% endblock %}
2844

29-
{% if drupal_major_version == 8 %}
45+
{% block location_drupal_legacy -%}
46+
{% if drupal_major_version == 8 %}
3047
# Redirect common PHP files to their new locations.
3148
location ~ ^((?!.*(?:core)).*)/(install.php|rebuild.php) {
3249
return 301 $scheme://$host$1/core/$2$is_args$args;
3350
}
34-
{% endif %}
51+
{% endif %}
52+
{% endblock %}
3553

54+
{% block location_php -%}
3655
# Use fastcgi for all php files.
3756
location ~ \.php$|^/update.php {
3857
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
@@ -43,36 +62,44 @@ server {
4362
include fastcgi_params;
4463
fastcgi_pass {{ php_fpm_listen }};
4564
}
65+
{% endblock %}
4666

67+
{% block location_rewrite -%}
4768
location @rewrite {
4869
rewrite ^ /index.php;
4970
}
71+
{% endblock %}
5072

73+
{% block location_image_styles -%}
5174
location ~ ^/sites/.*/files/styles/ {
5275
try_files $uri @rewrite;
5376
}
77+
{% endblock %}
5478

79+
{% block location_favicon -%}
5580
location = /favicon.ico {
5681
log_not_found off;
5782
access_log off;
5883
}
84+
{% endblock %}
5985

86+
{% block location_robots -%}
6087
location = /robots.txt {
6188
allow all;
6289
log_not_found off;
6390
access_log off;
6491
}
92+
{% endblock %}
6593

66-
location ~ (^|/)\. {
67-
return 403;
68-
}
69-
94+
{% block location_assets -%}
7095
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
7196
expires max;
7297
log_not_found off;
7398
}
99+
{% endblock %}
74100
{% endif %}
75101

102+
{% block compression -%}
76103
gzip on;
77104
gzip_proxied any;
78105
gzip_static on;
@@ -97,6 +124,7 @@ server {
97124
image/x-icon;
98125
gzip_buffers 16 8k;
99126
gzip_min_length 512;
127+
{% endblock %}
100128

101129
{% if item.extra_parameters is defined %}
102130
{{ item.extra_parameters }}

0 commit comments

Comments
 (0)