Skip to content

Commit 7d73476

Browse files
authored
Merge pull request #416 from ByteInternet/control_purge_method
Add article on controlling PURGE method
2 parents 16bb189 + 622fcde commit 7d73476

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
myst:
3+
html_meta:
4+
description: The PURGE method is disabled by default, learn how to safely enable
5+
it.
6+
title: How to Control PURGE Method | Hypernode
7+
---
8+
9+
# How to Control PURGE Method
10+
11+
Due to security and performance considerations, the `PURGE` method is disabled by default.
12+
13+
It's possible to enable it, but be careful to only open it to clients you trust. We'll show you how in this article.
14+
15+
## What is the PURGE Method?
16+
17+
The `PURGE` method is an HTTP request method used to clear cached content from reverse proxy caches like Varnish. When a `PURGE` request is sent to a URL, it instructs the cache server to remove the cached version of that content, forcing it to fetch a fresh copy from the origin server on the next request.
18+
19+
## Why is PURGE Disabled by Default?
20+
21+
By default, Hypernode blocks all `PURGE` requests through a configuration file located at `nginx/server.block_purge_requests.conf`.
22+
23+
This blocking is in place because:
24+
25+
- **Security Risk**: Without proper access controls, anyone could purge your cache, leading to performance degradation
26+
- **Performance Impact**: Malicious actors could repeatedly purge your cache, forcing your server to regenerate content constantly
27+
- **VCL Configuration Issues**: Many Varnish configurations use `client.ip` instead of `X-Real-IP` for ACL checks, which can allow unauthorized purging since requests appear to come from 127.0.0.1
28+
29+
## How to Safely Enable PURGE Method
30+
31+
If you need to enable the `PURGE` method for your application (e.g., for cache invalidation after content updates), you can do so by modifying or removing the blocking configuration. Here are the recommended approaches:
32+
33+
### Allow PURGE from Specific IP Addresses
34+
35+
The safest approach is to allow `PURGE` requests only from trusted IP addresses. Replace the content of `nginx/server.block_purge_requests.conf` with:
36+
37+
```nginx
38+
# Deny all PURGE requests by default, allow from specific IPs
39+
if ($request_method = PURGE) {
40+
set $purge_allowed 0;
41+
42+
if ($remote_addr = "10.0.0.10") {
43+
set $purge_allowed 1;
44+
}
45+
46+
if ($purge_allowed = 0) {
47+
return 401;
48+
}
49+
}
50+
```
51+
52+
### Allow PURGE with Authentication
53+
54+
You can require authentication for `PURGE` requests by checking for specific headers or parameters:
55+
56+
```nginx
57+
# Deny all, allow with authentication
58+
if ($request_method = PURGE) {
59+
set $purge_auth 0;
60+
61+
# Allow with proper authentication header/token
62+
if ($http_x_purge_token = "your-secret-token") {
63+
set $purge_auth 1;
64+
}
65+
66+
if ($purge_auth = 0) {
67+
return 401;
68+
}
69+
}
70+
```
71+
72+
### Comment Out the Block
73+
74+
For development environments only, you can temporarily disable the blocking by commenting out all lines.
75+
76+
```{caution}
77+
This approach removes all protection and should only be used in development environments.
78+
```
79+
80+
## Varnish Configuration
81+
82+
If you're using Varnish, ensure your VCL configuration properly handles purge requests and uses the real client IP:
83+
84+
```vcl
85+
import std;
86+
87+
acl purge {
88+
"127.0.0.1"; # allow localhost
89+
"10.0.0.0"/24; # allow your cluster private IP range
90+
}
91+
92+
sub vcl_recv {
93+
if (req.method == "PURGE") {
94+
set req.http.X-Purge-IP = std.ip(req.http.X-Real-IP, client.ip);
95+
if (std.ip(req.http.X-Purge-IP, "0.0.0.0") !~ purge) {
96+
return (synth(405));
97+
}
98+
return (purge);
99+
}
100+
}
101+
```

0 commit comments

Comments
 (0)