|
| 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 | +} |
0 commit comments