Skip to content

Commit 4e72650

Browse files
committed
Adding Varnish configuration for Platformsh
1 parent 3fcd26a commit 4e72650

File tree

3 files changed

+125
-17
lines changed

3 files changed

+125
-17
lines changed

.platform/default.vcl

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
vcl 4.0;
2+
import std;
3+
4+
# Hosts allowed to send BAN requests
5+
acl invalidators {
6+
"localhost";
7+
"127.0.0.1";
8+
# The 3 IPs below are the "fr-3" outbound IPs of Platform.sh
9+
# You should change this to the proper outbound IPs of your zone.
10+
"135.125.91.125";
11+
"135.125.89.47";
12+
"135.125.90.255";
13+
}
14+
15+
sub vcl_recv {
16+
# Route requests either to api or to the React admin app.
17+
if (req.url ~ "^/api(/|$)") {
18+
set req.backend_hint = api.backend();
19+
} else {
20+
set req.backend_hint = admin.backend();
21+
}
22+
23+
if (req.restarts > 0) {
24+
set req.hash_always_miss = true;
25+
}
26+
27+
# Remove the "Forwarded" HTTP header if exists (security)
28+
unset req.http.forwarded;
29+
30+
# To allow API Platform to ban by cache tags
31+
if (req.method == "BAN") {
32+
# The Platformsh router provides the real client IP as X-Client-IP
33+
# Use std.ip to convert the string to an IP for comparison
34+
35+
if (std.ip(req.http.X-Client-IP, "0.0.0.0") !~ invalidators) {
36+
# Uncomment this line if you want to restrict the purge to specific ips.
37+
# Do not forget to add the corresponding ips in the acl above.
38+
# return (synth(405, "Client Not allowed to ban."));
39+
}
40+
41+
if (req.http.ApiPlatform-Ban-Regex) {
42+
ban("obj.http.Cache-Tags ~ " + req.http.ApiPlatform-Ban-Regex);
43+
44+
return (synth(200, "Ban added"));
45+
}
46+
47+
return (synth(400, "ApiPlatform-Ban-Regex HTTP header must be set."));
48+
}
49+
50+
if (req.method != "GET" && req.method != "HEAD") {
51+
return (pass);
52+
}
53+
54+
# Do not cache "/" because it can be served by the Front (next.js) or API (ApiPlatform)
55+
# Temporary solution to avoid issue when Google crawls the website (see ESPP-378)
56+
if (req.url == "/") {
57+
return (pass);
58+
}
59+
60+
# Manage websocket
61+
if (req.http.upgrade ~ "(?i)websocket") {
62+
return (pipe);
63+
}
64+
65+
return (hash);
66+
}
67+
68+
sub vcl_pipe {
69+
if (req.http.upgrade) {
70+
set bereq.http.upgrade = req.http.upgrade;
71+
set bereq.http.connection = req.http.connection;
72+
}
73+
}
74+
75+
sub vcl_hit {
76+
if (obj.ttl >= 0s) {
77+
# A pure unadulterated hit, deliver it
78+
return (deliver);
79+
}
80+
81+
if (std.healthy(req.backend_hint)) {
82+
# The backend is healthy
83+
# Fetch the object from the backend
84+
return (restart);
85+
}
86+
87+
# No fresh object and the backend is not healthy
88+
if (obj.ttl + obj.grace > 0s) {
89+
# Deliver graced object
90+
# Automatically triggers a background fetch
91+
return (deliver);
92+
}
93+
94+
# No valid object to deliver
95+
# No healthy backend to handle request
96+
# Return error
97+
return (synth(503, "API is down"));
98+
}
99+
100+
sub vcl_deliver {
101+
# Don't send cache tags related headers to the client
102+
unset resp.http.url;
103+
# Comment the following line to send the "Cache-Tags" header to the client (e.g. to use CloudFlare cache tags)
104+
# unset resp.http.Cache-Tags;
105+
}
106+
107+
sub vcl_backend_response {
108+
# Ban lurker friendly header
109+
set beresp.http.url = bereq.url;
110+
111+
# Add a grace in case the backend is down
112+
set beresp.grace = 1h;
113+
}

.platform/routes.yaml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
# Route / to default React App.
1+
# Route everything to Varnish and he will dispatch.
22
https://{default}/:
3-
cache:
4-
cookies:
5-
- '*'
6-
default_ttl: 0
7-
enabled: true
8-
headers:
9-
- Accept
10-
- Accept-Language
11-
ssi:
12-
enabled: false
13-
type: upstream
14-
upstream: admin:http
15-
16-
# Route /api to Api platform app.
17-
https://{default}/api:
183
cache:
194
enabled: false
205
ssi:
216
enabled: false
227
type: upstream
23-
upstream: api:http
8+
upstream: varnish:http

.platform/services.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ opensearch:
1616
redis:
1717
type: redis-persistent:7.0
1818
disk: 256
19+
20+
varnish:
21+
type: varnish:7.6
22+
relationships:
23+
api: 'api:http'
24+
admin: 'admin:http'
25+
configuration:
26+
vcl: !include
27+
type: string
28+
path: default.vcl

0 commit comments

Comments
 (0)