Skip to content

Commit

Permalink
GH-420: Varnish for DDEV
Browse files Browse the repository at this point in the history
- Default ddev-varnish configuration
- The small vcl bit from .lando/varnish.vcl added to default.vcl
  • Loading branch information
sampoturve committed Aug 13, 2024
1 parent c489643 commit e57d748
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .ddev/addon-metadata/varnish/manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: varnish
repository: ddev/ddev-varnish
version: v0.2.3
install_date: "2024-08-13T15:59:06+03:00"
project_files:
- docker-compose.varnish.yaml
- varnish
- commands/varnish
global_files: []
removal_actions:
- |
#ddev-nodisplay
if [ -f docker-compose.varnish_extras.yaml ]; then
if grep -q '#ddev-generated' docker-compose.varnish_extras.yaml; then
rm -f docker-compose.varnish_extras.yaml
else
echo "Unwilling to remove '$DDEV_APPROOT/.ddev/docker-compose.varnish_extras.yaml' because it does not have #ddev-generated in it; you can manually delete it if it is safe to delete."
fi
fi
12 changes: 12 additions & 0 deletions .ddev/commands/varnish/varnishadm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

## #ddev-generated
## Description: Control a running Varnish instance
## Usage: varnishadm [flags] [args]
## Example: "ddev varnishadm"

# This example runs inside the varnish container.
# Note that this requires that /mnt/ddev_config be mounted
# into the varnish container.

varnishadm "$@"
14 changes: 14 additions & 0 deletions .ddev/commands/varnish/varnishd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

## #ddev-generated
## Description: Varnish-cli
## Usage: varnishd [flags] [args]
## Example: "ddev varnishd -d" for CLI in foreground.
## Example: "ddev varnishd -T" to connect with varnishadm or telnet.
## Example: "ddev varnishd -M" to connect back to a listening service pushing the CLI to that service.

# This example runs inside the varnish container.
# Note that this requires that /mnt/ddev_config be mounted
# into the varnish container.

varnishd "$@"
12 changes: 12 additions & 0 deletions .ddev/commands/varnish/varnishhist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

## #ddev-generated
## Description: Display Varnish request histogram
## Usage: varnishhist [flags] [args]
## Example: "ddev varnishhist"

# This example runs inside the varnish container.
# Note that this requires that /mnt/ddev_config be mounted
# into the varnish container.

varnishhist "$@"
12 changes: 12 additions & 0 deletions .ddev/commands/varnish/varnishlog
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

## #ddev-generated
## Description: Display Varnish logs
## Usage: varnishlog [flags] [args]
## Example: "ddev varnishlog"

# This example runs inside the varnish container.
# Note that this requires that /mnt/ddev_config be mounted
# into the varnish container.

varnishlog "$@"
12 changes: 12 additions & 0 deletions .ddev/commands/varnish/varnishncsa
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

## #ddev-generated
## Description: Display Varnish logs in Apache / NCSA combined log format
## Usage: varnishncsa [flags] [args]
## Example: "ddev varnishncsa"

# This example runs inside the varnish container.
# Note that this requires that /mnt/ddev_config be mounted
# into the varnish container.

varnishncsa "$@"
12 changes: 12 additions & 0 deletions .ddev/commands/varnish/varnishstat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

## #ddev-generated
## Description: Display Varnish Cache statistics
## Usage: varnishstat [flags] [args]
## Example: "ddev varnishstat"

# This example runs inside the varnish container.
# Note that this requires that /mnt/ddev_config be mounted
# into the varnish container.

varnishstat "$@"
12 changes: 12 additions & 0 deletions .ddev/commands/varnish/varnishtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

## #ddev-generated
## Description: Test program for Varnish
## Usage: varnishtest [flags] [args]
## Example: "ddev varnishtest"

# This example runs inside the varnish container.
# Note that this requires that /mnt/ddev_config be mounted
# into the varnish container.

varnishtest "$@"
12 changes: 12 additions & 0 deletions .ddev/commands/varnish/varnishtop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

## #ddev-generated
## Description: Display Varnish log entry ranking
## Usage: varnishtop [flags] [args]
## Example: "ddev varnishtop"

# This example runs inside the varnish container.
# Note that this requires that /mnt/ddev_config be mounted
# into the varnish container.

varnishtop "$@"
31 changes: 31 additions & 0 deletions .ddev/docker-compose.varnish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ddev-generated
services:
varnish:
container_name: ddev-${DDEV_SITENAME}-varnish
image: varnish:6.0
# These labels ensure this service is discoverable by ddev.
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.approot: $DDEV_APPROOT
environment:
# This defines the host name the service should be accessible from. This
# will be sitename.ddev.site.
# This is the first half of the trick that puts varnish "in front of" the
# web container, just by switching the names.
- VIRTUAL_HOST=$DDEV_HOSTNAME
# This defines the ports the service should be accessible from at
# sitename.ddev.site.
- HTTPS_EXPOSE=443:80,8026:8025
- HTTP_EXPOSE=80:80,8025:8025
volumes:
# This exposes a mount to the host system `.ddev/varnish` directory where
# your default.vcl should be.
- "./varnish:/etc/varnish"
- ".:/mnt/ddev_config"
depends_on:
- web
# Add mailhog support
expose:
- "8025"
entrypoint:
/usr/local/bin/docker-varnish-entrypoint -a 0.0.0.0:8025
7 changes: 7 additions & 0 deletions .ddev/docker-compose.varnish_extras.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ddev-generated
# This is the second half of the trick that puts varnish "in front of" the web
# container, just by switching the names.
services:
web:
environment:
- VIRTUAL_HOST=novarnish.drupal-project.ddev.site
38 changes: 38 additions & 0 deletions .ddev/varnish/default.vcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# For a more advanced example see https://github.com/mattiasgeniar/varnish-6.0-configuration-templates
vcl 4.1;
import std;

backend default {
.host = "web";
.port = "80";
}

# @see: https://github.com/wunderio/charts/blob/master/drupal/templates/varnish-configmap-vcl.yaml
# The routine when we deliver the HTTP request to the user
# Last chance to modify headers that are sent to the client.
sub vcl_deliver {
# Called before a cached object is delivered to the client.

# Add debug header to see if it's a HIT/MISS and the number of hits, disable when not needed.
if (obj.hits > 0) {
set resp.http.X-W-Cache = "HIT";
set resp.http.X-W-Cache-Hits = obj.hits;
} else {
set resp.http.X-W-Cache = "MISS";
}
}

sub vcl_recv {
if (std.port(server.ip) == 8025) {
return (synth(750));
}
}

sub vcl_synth {
if (resp.status == 750) {
set resp.status = 301;
set resp.http.location = req.http.X-Forwarded-Proto + "://novarnish." + req.http.Host + req.url;
set resp.reason = "Moved";
return (deliver);
}
}

0 comments on commit e57d748

Please sign in to comment.