You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/doc/administrate/cache.md
+139Lines changed: 139 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,145 @@ To enable it, create a `varnish.vcl` file in the `/clevercloud` folder. You can
29
29
The `vcl 4.1;` and backend section of the `varnish.vcl` configuration file are not necessary as they are already handled by Clever Cloud.
30
30
If you have a PHP FTP application or if your `varnish.vcl` file is on an FS Bucket, make sure you redeploy the application for the changes to take effect.
31
31
32
+
## Varnish to restrict access to your application
33
+
34
+
### Block IP addresses
35
+
36
+
```bash {filename="clevercloud/varnish.vcl"}
37
+
sub vcl_recv {
38
+
# Local health check
39
+
if (client.ip == "127.0.0.1"&&!req.http.X-Forwarded-For) {
40
+
return (synth(200, "OK"));
41
+
}
42
+
43
+
# We don't rely on client.ip which send the load balancer IP address
44
+
# We check if the IP to block is included in the Forwarded header instead
45
+
if (req.http.Forwarded ~"X.X.X.X") {
46
+
return (synth(403, "Blocked"));
47
+
}
48
+
49
+
# Use return (hash); to use the cache
50
+
return (pass);
51
+
}
52
+
53
+
sub vcl_synth {
54
+
if (resp.status == 403) {
55
+
set resp.http.Content-Type = "text/plain";
56
+
synthetic("Access denied");
57
+
return (deliver);
58
+
}
59
+
}
60
+
```
61
+
62
+
Replace `X.X.X.X` with the IP address you want to block.
63
+
64
+
If you want to block multiple IP addresses, you can use a regular expression like this:
65
+
66
+
```bash
67
+
if (req.http.Forwarded ~"^(X.X.X.X|Y.Y.Y.Y|Z.Z.Z.Z)$") {
68
+
return (synth(403, "Blocked"));
69
+
}
70
+
```
71
+
72
+
To be able to configure with an environment variable multiple IPs to block, CIDR, exceptions, etc. use the following example:
73
+
74
+
- [Varnish IP blocking with environment variable](https://github.com/CleverCloud/varnish-examples/blob/main/varnish-ip-blocking/varnish.vcl)
75
+
76
+
### Ask for a login/password (Basic authentication)
77
+
78
+
```bash {filename="clevercloud/varnish.vcl"}
79
+
sub vcl_recv {
80
+
# Local health check
81
+
if (client.ip == "127.0.0.1"&&!req.http.X-Forwarded-For) {
The `CREDENTIALS` string should be replaced with the base64 encoded value of `username:password`. You can use the following command to generate it on UNIX-based systems:
121
+
122
+
```bash
123
+
echo -n "username:password"| base64
124
+
```
125
+
126
+
### Bearer token authentication
127
+
128
+
```bash {filename="clevercloud/varnish.vcl"}
129
+
import env;
130
+
131
+
sub vcl_recv {
132
+
# Local health check
133
+
if (client.ip == "127.0.0.1"&&!req.http.X-Forwarded-For) {
134
+
return (synth(200, "OK"));
135
+
}
136
+
137
+
if (!req.http.Authorization) {
138
+
return (synth(401, "Authentication Required"));
139
+
}
140
+
141
+
if (req.http.Authorization !~ "^Bearer ") {
142
+
return (synth(401, "Bearer token Required"));
143
+
}
144
+
145
+
set req.http.X-Token = regsub(req.http.Authorization, "^Bearer ", "");
146
+
147
+
if (req.http.X-Token != env.get("CC_VARNISH_BEARER_TOKEN")) {
Once varnish is enabled, your application should no longer listen on port **8080**, but on port **8081**. Because it's Varnish that will listen on port **8080**, and it will have in its configuration your application as backend.
0 commit comments