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

Commit

Permalink
Fixes #97: Include Varnish in Drupal VM.
Browse files Browse the repository at this point in the history
  • Loading branch information
geerlingguy committed Jun 14, 2015
1 parent 45b6944 commit 2017889
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ It will install the following on an Ubuntu 14.04 (by default) linux VM:
- Drupal Console (if using Drupal 8+)
- Drupal 6.x, 7.x, or 8.x.x (configurable)
- Optional (installed by default):
- Varnish 4.x
- Apache Solr 4.10.x (configurable)
- Memcached
- XHProf, for profiling your code
Expand Down Expand Up @@ -82,6 +83,7 @@ By default, this VM includes the extras listed in the `config.yml` option `insta
- mailhog
- memcached
- solr
- varnish
- xdebug
- xhprof

Expand All @@ -96,6 +98,7 @@ Drupal VM is built to integrate with every developer's workflow. Many guides for
- [Use Apache Solr for Search](https://github.com/geerlingguy/drupal-vm/wiki/Use-Apache-Solr-for-Search)
- [Use Drush with Drupal VM](https://github.com/geerlingguy/drupal-vm/wiki/Use-Drush-with-Drupal-VM)
- [Use Drupal Console with Drupal VM](https://github.com/geerlingguy/drupal-vm/wiki/Use-Drupal-Console-with-Drupal-VM)
- [Use Varnish with Drupal VM](https://github.com/geerlingguy/drupal-vm/wiki/Use-Varnish-with-Drupal-VM)
- [View Logs with Pimp my Log](https://github.com/geerlingguy/drupal-vm/wiki/View-Logs-with-Pimp-my-Log)
- [Profile Code with XHProf](https://github.com/geerlingguy/drupal-vm/wiki/Profile-Code-with-XHProf)
- [Debug Code with XDebug](https://github.com/geerlingguy/drupal-vm/wiki/Debug-Code-with-XDebug)
Expand Down
8 changes: 8 additions & 0 deletions example.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ installed_extras:
- memcached
- pimpmylog
# - solr
- varnish
- xdebug
- xhprof

Expand All @@ -95,6 +96,7 @@ firewall_allowed_tcp_ports:
- "22"
- "25"
- "80"
- "81"
- "443"
- "8025"
- "8080"
Expand All @@ -118,6 +120,12 @@ mysql_slow_query_log_enabled: true
mysql_slow_query_time: 2
adminer_add_apache_config: true

# Varnish Configuration.
varnish_listen_port: "81"
varnish_default_vcl_template_path: provisioning/templates/drupalvm.vcl.j2
varnish_default_backend_host: "127.0.0.1"
varnish_default_backend_port: "80"

# Pimp my Log settings.
pimpmylog_install_dir: /usr/share/php/pimpmylog
pimpmylog_grant_all_privs: true
Expand Down
1 change: 1 addition & 0 deletions provisioning/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- { role: geerlingguy.mailhog, when: '"mailhog" in installed_extras' }
- { role: geerlingguy.java, when: '"solr" in installed_extras' }
- { role: geerlingguy.solr, when: '"solr" in installed_extras' }
- { role: geerlingguy.varnish, when: '"varnish" in installed_extras' }

# Roles for security and stability on production.
- { role: geerlingguy.security, when: extra_security_enabled }
Expand Down
1 change: 1 addition & 0 deletions provisioning/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ geerlingguy.pimpmylog
geerlingguy.repo-remi
geerlingguy.security
geerlingguy.solr
geerlingguy.varnish
91 changes: 91 additions & 0 deletions provisioning/templates/drupalvm.vcl.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
vcl 4.0;

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

# Default backend definition. Points to Apache, normally.
backend default {
.host = "{{ varnish_default_backend_host }}";
.port = "{{ varnish_default_backend_port }}";
.first_byte_timeout = 300s;
}

# Respond to incoming requests.
sub vcl_recv {
# Pass through any administrative or AJAX-related paths.
if (req.url ~ "^/status\.php$" ||
req.url ~ "^/update\.php$" ||
req.url ~ "^/admin$" ||
req.url ~ "^/admin/.*$" ||
req.url ~ "^/flag/.*$" ||
req.url ~ "^.*/ajax/.*$" ||
req.url ~ "^.*/ahah/.*$") {
return (pass);
}

# Removing cookies for static content so Varnish caches these files.
if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") {
unset req.http.Cookie;
}

# Remove all cookies that Drupal doesn't need to know about. We explicitly
# list the ones that Drupal does need, the SESS and NO_CACHE. If, after
# running this code we find that either of these two cookies remains, we
# will pass as the page cannot be cached.
if (req.http.Cookie) {
# 1. Append a semi-colon to the front of the cookie string.
# 2. Remove all spaces that appear after semi-colons.
# 3. Match the cookies we want to keep, adding the space we removed
# previously back. (\1) is first matching group in the regsuball.
# 4. Remove all other cookies, identifying them by the fact that they have
# no space after the preceding semi-colon.
# 5. Remove all spaces and semi-colons from the beginning and end of the
# cookie string.
set req.http.Cookie = ";" + req.http.Cookie;
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|SSESS[a-z0-9]+|NO_CACHE)=", "; \1=");
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

if (req.http.Cookie == "") {
# If there are no remaining cookies, remove the cookie header. If there
# aren't any cookie headers, Varnish's default behavior will be to cache
# the page.
unset req.http.Cookie;
}
else {
# If there is any cookies left (a session or NO_CACHE cookie), do not
# cache the page. Pass it on to Apache directly.
return (pass);
}
}
}

# Set a header to track a cache HITs and MISSes.
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Varnish-Cache = "HIT";
}
else {
set resp.http.X-Varnish-Cache = "MISS";
}
}

# Instruct Varnish what to do in the case of certain backend responses (beresp).
sub vcl_backend_response {
# Cache 404s, 301s, at 500s with a short lifetime to protect the backend.
if (beresp.status == 404 || beresp.status == 301 || beresp.status == 500) {
set beresp.ttl = 10m;
}

# Don't allow static files to set cookies.
# (?i) denotes case insensitive in PCRE (perl compatible regular expressions).
# This list of extensions appears twice, once here and again in vcl_recv so
# make sure you edit both and keep them equal.
if (bereq.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") {
unset beresp.http.set-cookie;
}

# Allow items to remain in cache up to 2 hours past their cache expiration.
set beresp.grace = 2h;
}

0 comments on commit 2017889

Please sign in to comment.