Skip to content

Commit 9fac77a

Browse files
authored
Merge pull request #2128 from gquintard/varnish_7.1
[varnish] add a few words about vmods
2 parents d02df8d + f8a0636 commit 9fac77a

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

varnish/content.md

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,45 @@ Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as w
1313
Create a `default.vcl` file:
1414

1515
```vcl
16-
vcl 4.0;
16+
# specify the VCL syntax version to use
17+
vcl 4.1;
1718
18-
backend default {
19-
.host = "www.nytimes.com:80";
19+
# import vmod_dynamic for better backend name resolution
20+
import dynamic;
21+
22+
# we won't use any static backend, but Varnish still need a default one
23+
backend default none;
24+
25+
# set up a dynamic director
26+
# for more info, see https://github.com/nigoroll/libvmod-dynamic/blob/master/src/vmod_dynamic.vcc
27+
sub vcl_init {
28+
new d = dynamic.director(port = "80");
29+
}
30+
31+
sub vcl_recv {
32+
# force the host header to match the backend (not all backends need it,
33+
# but example.com does)
34+
set req.http.host = "example.com";
35+
# set the backend
36+
set req.backend_hint = d.backend("example.com");
2037
}
2138
```
2239

2340
Then run:
2441

2542
```console
26-
# we need both a configuration file at /etc/varnish/default.vcl
27-
# and our workdir to be mounted as tmpfs to avoid disk I/O
28-
$ docker run -v /path/to/default.vcl:/etc/varnish/default.vcl:ro --tmpfs /var/lib/varnish/varnishd:exec %%IMAGE%%
43+
# we need the configuration file at /etc/varnish/default.vcl,
44+
# our workdir has to be mounted as tmpfs to avoid disk I/O,
45+
# and we'll use port 8080 to talk to our container (internally listening on 80)
46+
$ docker run \
47+
-v /path/to/default.vcl:/etc/varnish/default.vcl:ro \
48+
--tmpfs /var/lib/varnish/varnishd:exec \
49+
-p 8080:80 \
50+
%%IMAGE%%
2951
```
3052

53+
From there, you can visit `localhost:8080` in your browser and see the example.com homepage.
54+
3155
Alternatively, a simple `Dockerfile` can be used to generate a new image that includes the necessary `default.vcl` (which is a much cleaner solution than the bind mount above):
3256

3357
```dockerfile
@@ -39,7 +63,7 @@ COPY default.vcl /etc/varnish/
3963
Place this file in the same directory as your `default.vcl`, run `docker build -t my-varnish .`, then start your container:
4064

4165
```console
42-
$ docker --tmpfs /var/lib/varnish/varnishd:exec my-varnish
66+
$ docker --tmpfs /var/lib/varnish/varnishd:exec -p 8080:80 my-varnish
4367
```
4468

4569
## Reloading the configuration
@@ -64,14 +88,14 @@ docker run varnish varnishreload -h
6488
By default, the containers will use a cache size of 100MB, which is usually a bit too small, but you can quickly set it through the `VARNISH_SIZE` environment variable:
6589

6690
```console
67-
$ docker run --tmpfs /var/lib/varnish/varnishd:exec -e VARNISH_SIZE=2G %%IMAGE%%
91+
$ docker run --tmpfs /var/lib/varnish/varnishd:exec -p 8080:80 -e VARNISH_SIZE=2G %%IMAGE%%
6892
```
6993

7094
Additionally, you can add arguments to `docker run` after `%%IMAGE%%`, if the first one starts with a `-`, they will be appendend to the [default command](https://github.com/varnish/docker-varnish/blob/master/docker-varnish-entrypoint#L8):
7195

7296
```console
7397
# extend the default keep period
74-
$ docker run --tmpfs /var/lib/varnish/varnishd:exec -e VARNISH_SIZE=2G %%IMAGE%% -p default_keep=300
98+
$ docker run --tmpfs /var/lib/varnish/varnishd:exec -p 8080:80 -e VARNISH_SIZE=2G %%IMAGE%% -p default_keep=300
7599
```
76100

77101
If your first argument after `%%IMAGE%%` doesn't start with `-`, it will be interpreted as a command to override the default one:
@@ -84,13 +108,9 @@ $ docker run %%IMAGE%% varnishd -?
84108
$ docker run %%IMAGE%% varnishd -x parameter
85109

86110
# run the server with your own parameters (don't forget -F to not daemonize)
87-
$ docker run %%IMAGE%% varnishd -a :8080 -b 127.0.0.1:8181 -t 600 -p feature=+http2
111+
$ docker run %%IMAGE%% varnishd -F -a :8080 -b 127.0.0.1:8181 -t 600 -p feature=+http2
88112
```
89113

90-
## Exposing the port
91-
92-
```console
93-
$ docker run --name my-running-varnish --tmpfs /var/lib/varnish/varnishd:exec -d -p 8080:80 my-varnish
94-
```
114+
## vmods (since 7.1)
95115

96-
Then you can hit `http://localhost:8080` or `http://host-ip:8080` in your browser.
116+
As mentioned above, you can use [vmod_dynamic](https://github.com/nigoroll/libvmod-dynamic) for backend resolution. The [varnish-modules](https://github.com/varnish/varnish-modules) collection is also included in the image. All the documentation regarding usage and syntax can be found in the [src/](https://github.com/varnish/varnish-modules/tree/master/src) directory of the repository.

0 commit comments

Comments
 (0)