Skip to content
This repository was archived by the owner on Feb 13, 2023. It is now read-only.

Commit 8f00a4c

Browse files
committed
Issue #1254: Allow varnish vcl template to be overridden through template inheritance
1 parent 5beece3 commit 8f00a4c

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

docs/configurations/webservers-nginx.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ If you are using Ubuntu as your base OS and you want to get started quickly with
4545
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.
4646
4747
```yaml
48-
nginx_vhost_template: "{{ config_dir }}/nginx-vhost.conf.j2"
48+
nginx_vhost_template: "{{ config_dir }}/templates/nginx-vhost.conf.j2"
4949
```
5050
5151
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.

docs/extras/varnish.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,32 @@ $settings['reverse_proxy_addresses'] = array('127.0.0.1');
3131

3232
If you don't set these values, Drupal will think all requests are coming from `127.0.0.1`. There are other settings you can change to make Drupal not store copies of cached pages in the Database (since Varnish is caching everything, this is redundant), but those other settings are not covered here.
3333

34+
### Extending the base `drupalvm.vcl.j2` template
35+
36+
If you can't customize via variables because an option isn't exposed, you can extend the base `drupalvm.vcl.j2` through Jinja2 template inheritance.
37+
38+
```yaml
39+
varnish_default_vcl_template_path: "{{ config_dir }}/templates/drupalvm.vcl.j2"
40+
```
41+
42+
_If you extend Drupal VM's provided base template, the path referenced should to be relative to the playbook.yml._
43+
44+
```
45+
{% extends 'templates/drupalvm.vcl.j2' %}
46+
47+
{% block backend -%}
48+
{{ super() }}
49+
.connect_timeout = 1s;
50+
{% endblock %}
51+
52+
{% block vcl_deliver -%}
53+
unset resp.http.X-Url;
54+
unset resp.http.X-Host;
55+
unset resp.http.Purge-Cache-Tags;
56+
# Do not set X-Varnish-Cache headers.
57+
{% endblock %}
58+
```
59+
60+
The `{{ super() }}` Jinja2 function returns the original block content from the base template.
61+
3462
For a list of available role variables, see the [`geerlingguy.varnish` Ansible role's README](https://github.com/geerlingguy/ansible-role-varnish#readme).

provisioning/templates/drupalvm.vcl.j2

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1+
{% block version -%}
12
vcl 4.0;
3+
{% endblock %}
24

35
# This Varnish VCL has been adapted from the Four Kitchens VCL for Varnish 3.
46

57
# Default backend definition. Points to Apache, normally.
68
backend default {
9+
{% block backend -%}
710
.host = "{{ varnish_default_backend_host }}";
811
.port = "{{ varnish_default_backend_port }}";
912
.first_byte_timeout = 300s;
13+
{% endblock %}
1014
}
1115

1216
# Access control list for PURGE requests.
1317
acl purge {
18+
{% block acl_purge -%}
1419
"127.0.0.1";
20+
{% endblock %}
1521
}
1622

1723
# Respond to incoming requests.
1824
sub vcl_recv {
25+
{% block http_header -%}
1926
# Add an X-Forwarded-For header with the client IP address.
2027
if (req.restarts == 0) {
2128
if (req.http.X-Forwarded-For) {
@@ -25,15 +32,19 @@ sub vcl_recv {
2532
set req.http.X-Forwarded-For = client.ip;
2633
}
2734
}
35+
{% endblock %}
2836

37+
{% block method_purge -%}
2938
# Only allow PURGE requests from IP addresses in the 'purge' ACL.
3039
if (req.method == "PURGE") {
3140
if (!client.ip ~ purge) {
3241
return (synth(405, "Not allowed."));
3342
}
3443
return (purge);
3544
}
45+
{% endblock %}
3646

47+
{% block method_ban -%}
3748
# Only allow BAN requests from IP addresses in the 'purge' ACL.
3849
if (req.method == "BAN") {
3950
# Same ACL check as above:
@@ -53,12 +64,16 @@ sub vcl_recv {
5364
# Throw a synthetic page so the request won't go to the backend.
5465
return (synth(200, "Ban added."));
5566
}
67+
{% endblock %}
5668

69+
{% block pass_post -%}
5770
# Only cache GET and HEAD requests (pass through POST requests).
5871
if (req.method != "GET" && req.method != "HEAD") {
5972
return (pass);
6073
}
74+
{% endblock %}
6175

76+
{% block pass_admin -%}
6277
# Pass through any administrative or AJAX-related paths.
6378
if (req.url ~ "^/status\.php$" ||
6479
req.url ~ "^/update\.php$" ||
@@ -69,7 +84,9 @@ sub vcl_recv {
6984
req.url ~ "^.*/ahah/.*$") {
7085
return (pass);
7186
}
87+
{% endblock %}
7288

89+
{% block cookies -%}
7390
# Removing cookies for static content so Varnish caches these files.
7491
if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") {
7592
unset req.http.Cookie;
@@ -106,10 +123,12 @@ sub vcl_recv {
106123
return (pass);
107124
}
108125
}
126+
{% endblock %}
109127
}
110128

111129
# Set a header to track a cache HITs and MISSes.
112130
sub vcl_deliver {
131+
{% block vcl_deliver -%}
113132
# Remove ban-lurker friendly custom headers when delivering to client.
114133
unset resp.http.X-Url;
115134
unset resp.http.X-Host;
@@ -121,10 +140,12 @@ sub vcl_deliver {
121140
else {
122141
set resp.http.X-Varnish-Cache = "MISS";
123142
}
143+
{% endblock %}
124144
}
125145

126146
# Instruct Varnish what to do in the case of certain backend responses (beresp).
127147
sub vcl_backend_response {
148+
{% block vcl_backend_response -%}
128149
# Set ban-lurker friendly custom headers.
129150
set beresp.http.X-Url = bereq.url;
130151
set beresp.http.X-Host = bereq.http.host;
@@ -150,4 +171,5 @@ sub vcl_backend_response {
150171

151172
# Allow items to remain in cache up to 6 hours past their cache expiration.
152173
set beresp.grace = 6h;
174+
{% endblock %}
153175
}

0 commit comments

Comments
 (0)