Skip to content

Commit 41f37f6

Browse files
committed
Add ability to purge content from SCGI's cache.
1 parent 70e2480 commit 41f37f6

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* Fix various build scenarios with disabled upstream modules.
33
Reported by Johan Bergstroem.
44

5+
* Add ability to purge content from SCGI's cache.
6+
Requested by Johan Bergstroem.
7+
58
2010-06-08 VERSION 1.1
69
* Fix compatibility with nginx-0.8.40+.
710

config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ if [ "$HTTP_FASTCGI" = "YES" ]; then
22
have=NGX_HTTP_FASTCGI . auto/have
33
fi
44

5+
if [ "$HTTP_SCGI" = "YES" ]; then
6+
have=NGX_HTTP_SCGI . auto/have
7+
fi
8+
59
if [ "$HTTP_UWSGI" = "YES" ]; then
610
have=NGX_HTTP_UWSGI . auto/have
711
fi
812

913
ngx_addon_name=ngx_http_cache_purge_module
1014
HTTP_MODULES="$HTTP_MODULES ngx_http_cache_purge_module"
1115
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_cache_purge_module.c"
16+
17+
have=NGX_CACHE_PURGE_MODULE . auto/have

ngx_cache_purge_module.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ char *ngx_http_proxy_cache_purge_conf(ngx_conf_t *cf,
4646
ngx_int_t ngx_http_proxy_cache_purge_handler(ngx_http_request_t *r);
4747
# endif /* NGX_HTTP_PROXY */
4848

49+
# if (NGX_HTTP_SCGI)
50+
char *ngx_http_scgi_cache_purge_conf(ngx_conf_t *cf,
51+
ngx_command_t *cmd, void *conf);
52+
ngx_int_t ngx_http_scgi_cache_purge_handler(ngx_http_request_t *r);
53+
# endif /* NGX_HTTP_SCGI */
54+
4955
# if (NGX_HTTP_UWSGI)
5056
char *ngx_http_uwsgi_cache_purge_conf(ngx_conf_t *cf,
5157
ngx_command_t *cmd, void *conf);
@@ -78,6 +84,15 @@ static ngx_command_t ngx_http_cache_purge_module_commands[] = {
7884
NULL },
7985
# endif /* NGX_HTTP_PROXY */
8086

87+
# if (NGX_HTTP_SCGI)
88+
{ ngx_string("scgi_cache_purge"),
89+
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
90+
ngx_http_scgi_cache_purge_conf,
91+
NGX_HTTP_LOC_CONF_OFFSET,
92+
0,
93+
NULL },
94+
# endif /* NGX_HTTP_SCGI */
95+
8196
# if (NGX_HTTP_UWSGI)
8297
{ ngx_string("uwsgi_cache_purge"),
8398
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
@@ -349,6 +364,95 @@ ngx_http_proxy_cache_purge_handler(ngx_http_request_t *r)
349364
}
350365
# endif /* NGX_HTTP_PROXY */
351366

367+
# if (NGX_HTTP_SCGI)
368+
extern ngx_module_t ngx_http_scgi_module;
369+
370+
typedef struct {
371+
ngx_http_upstream_conf_t upstream;
372+
373+
ngx_array_t *flushes;
374+
ngx_array_t *params_len;
375+
ngx_array_t *params;
376+
ngx_array_t *params_source;
377+
378+
ngx_hash_t headers_hash;
379+
ngx_uint_t header_params;
380+
381+
ngx_array_t *scgi_lengths;
382+
ngx_array_t *scgi_values;
383+
384+
ngx_http_complex_value_t cache_key;
385+
} ngx_http_scgi_loc_conf_t;
386+
387+
char *
388+
ngx_http_scgi_cache_purge_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
389+
{
390+
ngx_http_compile_complex_value_t ccv;
391+
ngx_http_core_loc_conf_t *clcf;
392+
ngx_http_scgi_loc_conf_t *slcf;
393+
ngx_str_t *value;
394+
395+
slcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_scgi_module);
396+
397+
/* check for duplicates / collisions */
398+
if (slcf->upstream.cache != NGX_CONF_UNSET_PTR
399+
&& slcf->upstream.cache != NULL)
400+
{
401+
return "is either duplicate or collides with \"scgi_cache\"";
402+
}
403+
404+
if (slcf->upstream.upstream || slcf->scgi_lengths) {
405+
return "is incompatible with \"scgi_pass\"";
406+
}
407+
408+
if (slcf->upstream.store > 0 || slcf->upstream.store_lengths) {
409+
return "is incompatible with \"scgi_store\"";
410+
}
411+
412+
value = cf->args->elts;
413+
414+
/* set scgi_cache part */
415+
slcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
416+
&ngx_http_scgi_module);
417+
if (slcf->upstream.cache == NULL) {
418+
return NGX_CONF_ERROR;
419+
}
420+
421+
/* set scgi_cache_key part */
422+
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
423+
424+
ccv.cf = cf;
425+
ccv.value = &value[2];
426+
ccv.complex_value = &slcf->cache_key;
427+
428+
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
429+
return NGX_CONF_ERROR;
430+
}
431+
432+
/* set handler */
433+
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
434+
435+
clcf->handler = ngx_http_scgi_cache_purge_handler;
436+
437+
return NGX_CONF_OK;
438+
}
439+
440+
ngx_int_t
441+
ngx_http_scgi_cache_purge_handler(ngx_http_request_t *r)
442+
{
443+
ngx_http_scgi_loc_conf_t *slcf;
444+
445+
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_DELETE))) {
446+
return NGX_HTTP_NOT_ALLOWED;
447+
}
448+
449+
slcf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);
450+
451+
return ngx_http_cache_purge_handler(r, slcf->upstream.cache->data,
452+
&slcf->cache_key);
453+
}
454+
# endif /* NGX_HTTP_SCGI */
455+
352456
# if (NGX_HTTP_UWSGI)
353457
extern ngx_module_t ngx_http_uwsgi_module;
354458

0 commit comments

Comments
 (0)