Skip to content

Commit d167e1b

Browse files
committed
Catch all purge requests, even if cache isn't configured.
When enabled, cache purge will now catch all requests with PURGE (or specified) method, even if cache isn't configured. Previously, it would pass such requests to the upstream. Change-Id: Ic8ebf3eb22ce5862e98f7844670a096f56065746 Signed-off-by: Piotr Sikora <[email protected]>
1 parent a0d8a34 commit d167e1b

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

ngx_cache_purge_module.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,10 @@ ngx_http_cache_purge_access_handler(ngx_http_request_t *r)
716716
return NGX_HTTP_FORBIDDEN;
717717
}
718718

719+
if (cplcf->handler == NULL) {
720+
return NGX_HTTP_NOT_FOUND;
721+
}
722+
719723
return cplcf->handler(r);
720724
}
721725

@@ -1184,11 +1188,10 @@ ngx_http_cache_purge_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
11841188
if (conf->fastcgi.enable && clcf->handler != NULL) {
11851189
flcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_fastcgi_module);
11861190

1187-
if (flcf->upstream.cache
1188-
&& (flcf->upstream.upstream || flcf->fastcgi_lengths))
1189-
{
1191+
if (flcf->upstream.upstream || flcf->fastcgi_lengths) {
11901192
conf->conf = &conf->fastcgi;
1191-
conf->handler = ngx_http_fastcgi_cache_purge_handler;
1193+
conf->handler = flcf->upstream.cache
1194+
? ngx_http_fastcgi_cache_purge_handler : NULL;
11921195
conf->original_handler = clcf->handler;
11931196

11941197
clcf->handler = ngx_http_cache_purge_access_handler;
@@ -1204,11 +1207,10 @@ ngx_http_cache_purge_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
12041207
if (conf->proxy.enable && clcf->handler != NULL) {
12051208
plcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_proxy_module);
12061209

1207-
if (plcf->upstream.cache
1208-
&& (plcf->upstream.upstream || plcf->proxy_lengths))
1209-
{
1210+
if (plcf->upstream.upstream || plcf->proxy_lengths) {
12101211
conf->conf = &conf->proxy;
1211-
conf->handler = ngx_http_proxy_cache_purge_handler;
1212+
conf->handler = plcf->upstream.cache
1213+
? ngx_http_proxy_cache_purge_handler : NULL;
12121214
conf->original_handler = clcf->handler;
12131215

12141216
clcf->handler = ngx_http_cache_purge_access_handler;
@@ -1224,13 +1226,11 @@ ngx_http_cache_purge_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
12241226
if (conf->scgi.enable && clcf->handler != NULL) {
12251227
slcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_scgi_module);
12261228

1227-
if (slcf->upstream.cache
1228-
&& (slcf->upstream.upstream || slcf->scgi_lengths))
1229-
{
1229+
if (slcf->upstream.upstream || slcf->scgi_lengths) {
12301230
conf->conf = &conf->scgi;
1231-
conf->handler = ngx_http_scgi_cache_purge_handler;
1231+
conf->handler = slcf->upstream.cache
1232+
? ngx_http_scgi_cache_purge_handler : NULL;
12321233
conf->original_handler = clcf->handler;
1233-
12341234
clcf->handler = ngx_http_cache_purge_access_handler;
12351235

12361236
return NGX_CONF_OK;
@@ -1244,11 +1244,10 @@ ngx_http_cache_purge_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
12441244
if (conf->uwsgi.enable && clcf->handler != NULL) {
12451245
ulcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_uwsgi_module);
12461246

1247-
if (ulcf->upstream.cache
1248-
&& (ulcf->upstream.upstream || ulcf->uwsgi_lengths))
1249-
{
1247+
if (ulcf->upstream.upstream || ulcf->uwsgi_lengths) {
12501248
conf->conf = &conf->uwsgi;
1251-
conf->handler = ngx_http_uwsgi_cache_purge_handler;
1249+
conf->handler = ulcf->upstream.cache
1250+
? ngx_http_uwsgi_cache_purge_handler : NULL;
12521251
conf->original_handler = clcf->handler;
12531252

12541253
clcf->handler = ngx_http_cache_purge_access_handler;

t/proxy2.t

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,79 @@ X-Cache-Status: HIT
241241
--- response_body_like: root
242242
--- timeout: 10
243243
--- skip_nginx2: 4: < 0.8.3 or < 0.7.62
244+
245+
246+
247+
=== TEST 13: no cache (PURGE allowed)
248+
--- http_config eval: $::http_config
249+
--- config
250+
proxy_cache_purge PURGE from 1.0.0.0/8 127.0.0.0/8 3.0.0.0/8;
251+
252+
location /proxy {
253+
proxy_pass $scheme://127.0.0.1:$server_port/etc/passwd;
254+
}
255+
256+
location = /etc/passwd {
257+
root /;
258+
}
259+
--- request
260+
PURGE /proxy/passwd
261+
--- error_code: 404
262+
--- response_headers
263+
Content-Type: text/html
264+
--- response_body_like: 404 Not Found
265+
--- timeout: 10
266+
--- skip_nginx2: 4: < 0.8.3 or < 0.7.62
267+
268+
269+
270+
=== TEST 14: no cache (PURGE not allowed)
271+
--- http_config eval: $::http_config
272+
--- config
273+
proxy_cache_purge PURGE from 1.0.0.0/8;
274+
275+
location /proxy {
276+
proxy_pass $scheme://127.0.0.1:$server_port/etc/passwd;
277+
}
278+
279+
location = /etc/passwd {
280+
root /;
281+
}
282+
--- request
283+
PURGE /proxy/passwd
284+
--- error_code: 403
285+
--- response_headers
286+
Content-Type: text/html
287+
--- response_body_like: 403 Forbidden
288+
--- timeout: 10
289+
--- skip_nginx2: 4: < 0.8.3 or < 0.7.62
290+
291+
292+
293+
=== TEST 15: multiple cache purge directives
294+
--- http_config eval: $::http_config
295+
--- config
296+
fastcgi_cache_purge on;
297+
proxy_cache_purge on;
298+
299+
location /proxy {
300+
proxy_pass $scheme://127.0.0.1:$server_port/etc/passwd;
301+
proxy_cache test_cache;
302+
proxy_cache_key $uri$is_args$args;
303+
proxy_cache_valid 3m;
304+
add_header X-Cache-Status $upstream_cache_status;
305+
306+
if ($uri) { }
307+
}
308+
309+
location = /etc/passwd {
310+
root /;
311+
}
312+
--- request
313+
PURGE /proxy/passwd
314+
--- error_code: 200
315+
--- response_headers
316+
Content-Type: text/html
317+
--- response_body_like: Successful purge
318+
--- timeout: 10
319+
--- skip_nginx2: 4: < 0.8.3 or < 0.7.62

0 commit comments

Comments
 (0)