From c40a72761c909aca80fb27d9df12fa3043afbaa2 Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Mon, 22 Dec 2014 12:59:02 +0300 Subject: [PATCH] Upstream: refactored proxy_cache and friends. The configuration handling code has changed to look similar to the proxy_store directive and friends. This simplifies adding variable support in the following patch. No functional changes. --- src/http/modules/ngx_http_fastcgi_module.c | 39 +++++++++++----------- src/http/modules/ngx_http_proxy_module.c | 39 +++++++++++----------- src/http/modules/ngx_http_scgi_module.c | 38 ++++++++++----------- src/http/modules/ngx_http_uwsgi_module.c | 36 ++++++++++---------- src/http/ngx_http_upstream.c | 6 ++-- src/http/ngx_http_upstream.h | 5 ++- 6 files changed, 85 insertions(+), 78 deletions(-) diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c index aa2dcd7e5..39e9cd0aa 100644 --- a/src/http/modules/ngx_http_fastcgi_module.c +++ b/src/http/modules/ngx_http_fastcgi_module.c @@ -2352,6 +2352,7 @@ ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf) * conf->upstream.bufs.num = 0; * conf->upstream.ignore_headers = 0; * conf->upstream.next_upstream = 0; + * conf->upstream.cache_zone = NULL; * conf->upstream.cache_use_stale = 0; * conf->upstream.cache_methods = 0; * conf->upstream.temp_path = NULL; @@ -2390,7 +2391,7 @@ ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.pass_request_body = NGX_CONF_UNSET; #if (NGX_HTTP_CACHE) - conf->upstream.cache = NGX_CONF_UNSET_PTR; + conf->upstream.cache = NGX_CONF_UNSET; conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT; conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR; conf->upstream.no_cache = NGX_CONF_UNSET_PTR; @@ -2435,12 +2436,10 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) #if (NGX_HTTP_CACHE) if (conf->upstream.store > 0) { - conf->upstream.cache = NULL; + conf->upstream.cache = 0; } - if (conf->upstream.cache != NGX_CONF_UNSET_PTR - && conf->upstream.cache != NULL) - { + if (conf->upstream.cache > 0) { conf->upstream.store = 0; } @@ -2613,13 +2612,17 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) #if (NGX_HTTP_CACHE) - ngx_conf_merge_ptr_value(conf->upstream.cache, - prev->upstream.cache, NULL); + if (conf->upstream.cache == NGX_CONF_UNSET) { + ngx_conf_merge_value(conf->upstream.cache, + prev->upstream.cache, 0); - if (conf->upstream.cache && conf->upstream.cache->data == NULL) { + conf->upstream.cache_zone = prev->upstream.cache_zone; + } + + if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) { ngx_shm_zone_t *shm_zone; - shm_zone = conf->upstream.cache; + shm_zone = conf->upstream.cache_zone; ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"fastcgi_cache\" zone \"%V\" is unknown", @@ -3230,13 +3233,9 @@ ngx_http_fastcgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } #if (NGX_HTTP_CACHE) - - if (flcf->upstream.cache != NGX_CONF_UNSET_PTR - && flcf->upstream.cache != NULL) - { + if (flcf->upstream.cache > 0) { return "is incompatible with \"fastcgi_cache\""; } - #endif flcf->upstream.store = 1; @@ -3277,12 +3276,12 @@ ngx_http_fastcgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) value = cf->args->elts; - if (flcf->upstream.cache != NGX_CONF_UNSET_PTR) { + if (flcf->upstream.cache != NGX_CONF_UNSET) { return "is duplicate"; } if (ngx_strcmp(value[1].data, "off") == 0) { - flcf->upstream.cache = NULL; + flcf->upstream.cache = 0; return NGX_CONF_OK; } @@ -3290,9 +3289,11 @@ ngx_http_fastcgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return "is incompatible with \"fastcgi_store\""; } - flcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0, - &ngx_http_fastcgi_module); - if (flcf->upstream.cache == NULL) { + flcf->upstream.cache = 1; + + flcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0, + &ngx_http_fastcgi_module); + if (flcf->upstream.cache_zone == NULL) { return NGX_CONF_ERROR; } diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c index 69a704cbc..a3985b93d 100644 --- a/src/http/modules/ngx_http_proxy_module.c +++ b/src/http/modules/ngx_http_proxy_module.c @@ -2509,6 +2509,7 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) * conf->upstream.bufs.num = 0; * conf->upstream.ignore_headers = 0; * conf->upstream.next_upstream = 0; + * conf->upstream.cache_zone = NULL; * conf->upstream.cache_use_stale = 0; * conf->upstream.cache_methods = 0; * conf->upstream.temp_path = NULL; @@ -2566,7 +2567,7 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) conf->upstream.pass_request_body = NGX_CONF_UNSET; #if (NGX_HTTP_CACHE) - conf->upstream.cache = NGX_CONF_UNSET_PTR; + conf->upstream.cache = NGX_CONF_UNSET; conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT; conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR; conf->upstream.no_cache = NGX_CONF_UNSET_PTR; @@ -2627,12 +2628,10 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) #if (NGX_HTTP_CACHE) if (conf->upstream.store > 0) { - conf->upstream.cache = NULL; + conf->upstream.cache = 0; } - if (conf->upstream.cache != NGX_CONF_UNSET_PTR - && conf->upstream.cache != NULL) - { + if (conf->upstream.cache > 0) { conf->upstream.store = 0; } @@ -2804,13 +2803,17 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) #if (NGX_HTTP_CACHE) - ngx_conf_merge_ptr_value(conf->upstream.cache, - prev->upstream.cache, NULL); + if (conf->upstream.cache == NGX_CONF_UNSET) { + ngx_conf_merge_value(conf->upstream.cache, + prev->upstream.cache, 0); - if (conf->upstream.cache && conf->upstream.cache->data == NULL) { + conf->upstream.cache_zone = prev->upstream.cache_zone; + } + + if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) { ngx_shm_zone_t *shm_zone; - shm_zone = conf->upstream.cache; + shm_zone = conf->upstream.cache_zone; ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"proxy_cache\" zone \"%V\" is unknown", @@ -3818,13 +3821,9 @@ ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } #if (NGX_HTTP_CACHE) - - if (plcf->upstream.cache != NGX_CONF_UNSET_PTR - && plcf->upstream.cache != NULL) - { + if (plcf->upstream.cache > 0) { return "is incompatible with \"proxy_cache\""; } - #endif plcf->upstream.store = 1; @@ -3865,12 +3864,12 @@ ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) value = cf->args->elts; - if (plcf->upstream.cache != NGX_CONF_UNSET_PTR) { + if (plcf->upstream.cache != NGX_CONF_UNSET) { return "is duplicate"; } if (ngx_strcmp(value[1].data, "off") == 0) { - plcf->upstream.cache = NULL; + plcf->upstream.cache = 0; return NGX_CONF_OK; } @@ -3878,9 +3877,11 @@ ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return "is incompatible with \"proxy_store\""; } - plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0, - &ngx_http_proxy_module); - if (plcf->upstream.cache == NULL) { + plcf->upstream.cache = 1; + + plcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0, + &ngx_http_proxy_module); + if (plcf->upstream.cache_zone == NULL) { return NGX_CONF_ERROR; } diff --git a/src/http/modules/ngx_http_scgi_module.c b/src/http/modules/ngx_http_scgi_module.c index fed916ace..845fcb4bb 100644 --- a/src/http/modules/ngx_http_scgi_module.c +++ b/src/http/modules/ngx_http_scgi_module.c @@ -1148,7 +1148,7 @@ ngx_http_scgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.pass_request_body = NGX_CONF_UNSET; #if (NGX_HTTP_CACHE) - conf->upstream.cache = NGX_CONF_UNSET_PTR; + conf->upstream.cache = NGX_CONF_UNSET; conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT; conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR; conf->upstream.no_cache = NGX_CONF_UNSET_PTR; @@ -1189,12 +1189,10 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) #if (NGX_HTTP_CACHE) if (conf->upstream.store > 0) { - conf->upstream.cache = NULL; + conf->upstream.cache = 0; } - if (conf->upstream.cache != NGX_CONF_UNSET_PTR - && conf->upstream.cache != NULL) - { + if (conf->upstream.cache > 0) { conf->upstream.store = 0; } @@ -1366,13 +1364,17 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) #if (NGX_HTTP_CACHE) - ngx_conf_merge_ptr_value(conf->upstream.cache, - prev->upstream.cache, NULL); + if (conf->upstream.cache == NGX_CONF_UNSET) { + ngx_conf_merge_value(conf->upstream.cache, + prev->upstream.cache, 0); - if (conf->upstream.cache && conf->upstream.cache->data == NULL) { + conf->upstream.cache_zone = prev->upstream.cache_zone; + } + + if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) { ngx_shm_zone_t *shm_zone; - shm_zone = conf->upstream.cache; + shm_zone = conf->upstream.cache_zone; ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"scgi_cache\" zone \"%V\" is unknown", @@ -1784,13 +1786,9 @@ ngx_http_scgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } #if (NGX_HTTP_CACHE) - - if (scf->upstream.cache != NGX_CONF_UNSET_PTR - && scf->upstream.cache != NULL) - { + if (scf->upstream.cache > 0) { return "is incompatible with \"scgi_cache\""; } - #endif scf->upstream.store = 1; @@ -1831,12 +1829,12 @@ ngx_http_scgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) value = cf->args->elts; - if (scf->upstream.cache != NGX_CONF_UNSET_PTR) { + if (scf->upstream.cache != NGX_CONF_UNSET) { return "is duplicate"; } if (ngx_strcmp(value[1].data, "off") == 0) { - scf->upstream.cache = NULL; + scf->upstream.cache = 0; return NGX_CONF_OK; } @@ -1844,9 +1842,11 @@ ngx_http_scgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return "is incompatible with \"scgi_store\""; } - scf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0, - &ngx_http_scgi_module); - if (scf->upstream.cache == NULL) { + scf->upstream.cache = 1; + + scf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0, + &ngx_http_scgi_module); + if (scf->upstream.cache_zone == NULL) { return NGX_CONF_ERROR; } diff --git a/src/http/modules/ngx_http_uwsgi_module.c b/src/http/modules/ngx_http_uwsgi_module.c index 85db42226..a8e2ef250 100644 --- a/src/http/modules/ngx_http_uwsgi_module.c +++ b/src/http/modules/ngx_http_uwsgi_module.c @@ -1354,7 +1354,7 @@ ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.pass_request_body = NGX_CONF_UNSET; #if (NGX_HTTP_CACHE) - conf->upstream.cache = NGX_CONF_UNSET_PTR; + conf->upstream.cache = NGX_CONF_UNSET; conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT; conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR; conf->upstream.no_cache = NGX_CONF_UNSET_PTR; @@ -1403,12 +1403,10 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) #if (NGX_HTTP_CACHE) if (conf->upstream.store > 0) { - conf->upstream.cache = NULL; + conf->upstream.cache = 0; } - if (conf->upstream.cache != NGX_CONF_UNSET_PTR - && conf->upstream.cache != NULL) - { + if (conf->upstream.cache > 0) { conf->upstream.store = 0; } @@ -1580,13 +1578,17 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) #if (NGX_HTTP_CACHE) - ngx_conf_merge_ptr_value(conf->upstream.cache, - prev->upstream.cache, NULL); + if (conf->upstream.cache == NGX_CONF_UNSET) { + ngx_conf_merge_value(conf->upstream.cache, + prev->upstream.cache, 0); - if (conf->upstream.cache && conf->upstream.cache->data == NULL) { + conf->upstream.cache_zone = prev->upstream.cache_zone; + } + + if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) { ngx_shm_zone_t *shm_zone; - shm_zone = conf->upstream.cache; + shm_zone = conf->upstream.cache_zone; ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"uwsgi_cache\" zone \"%V\" is unknown", @@ -2072,9 +2074,7 @@ ngx_http_uwsgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) #if (NGX_HTTP_CACHE) - if (uwcf->upstream.cache != NGX_CONF_UNSET_PTR - && uwcf->upstream.cache != NULL) - { + if (uwcf->upstream.cache > 0) { return "is incompatible with \"uwsgi_cache\""; } @@ -2118,12 +2118,12 @@ ngx_http_uwsgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) value = cf->args->elts; - if (uwcf->upstream.cache != NGX_CONF_UNSET_PTR) { + if (uwcf->upstream.cache != NGX_CONF_UNSET) { return "is duplicate"; } if (ngx_strcmp(value[1].data, "off") == 0) { - uwcf->upstream.cache = NULL; + uwcf->upstream.cache = 0; return NGX_CONF_OK; } @@ -2131,9 +2131,11 @@ ngx_http_uwsgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return "is incompatible with \"uwsgi_store\""; } - uwcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0, - &ngx_http_uwsgi_module); - if (uwcf->upstream.cache == NULL) { + uwcf->upstream.cache = 1; + + uwcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0, + &ngx_http_uwsgi_module); + if (uwcf->upstream.cache_zone == NULL) { return NGX_CONF_ERROR; } diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c index 0693f0529..b2066a3ef 100644 --- a/src/http/ngx_http_upstream.c +++ b/src/http/ngx_http_upstream.c @@ -780,7 +780,7 @@ ngx_http_upstream_cache(ngx_http_request_t *r, ngx_http_upstream_t *u) c->min_uses = u->conf->cache_min_uses; c->body_start = u->conf->buffer_size; - c->file_cache = u->conf->cache->data; + c->file_cache = u->conf->cache_zone->data; c->lock = u->conf->cache_lock; c->lock_timeout = u->conf->cache_lock_timeout; @@ -2538,7 +2538,7 @@ ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u) r->cache->min_uses = u->conf->cache_min_uses; r->cache->body_start = u->conf->buffer_size; - r->cache->file_cache = u->conf->cache->data; + r->cache->file_cache = u->conf->cache_zone->data; if (ngx_http_file_cache_create(r) != NGX_OK) { ngx_http_upstream_finalize_request(r, u, NGX_ERROR); @@ -5545,7 +5545,7 @@ ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf, if (conf->hide_headers_hash.buckets #if (NGX_HTTP_CACHE) - && ((conf->cache == NULL) == (prev->cache == NULL)) + && ((conf->cache == 0) == (prev->cache == 0)) #endif ) { diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h index d04b66906..23db15aea 100644 --- a/src/http/ngx_http_upstream.h +++ b/src/http/ngx_http_upstream.h @@ -175,7 +175,7 @@ typedef struct { ngx_http_upstream_local_t *local; #if (NGX_HTTP_CACHE) - ngx_shm_zone_t *cache; + ngx_shm_zone_t *cache_zone; ngx_uint_t cache_min_uses; ngx_uint_t cache_use_stale; @@ -195,6 +195,9 @@ typedef struct { ngx_array_t *store_lengths; ngx_array_t *store_values; +#if (NGX_HTTP_CACHE) + signed cache:2; +#endif signed store:2; unsigned intercept_404:1; unsigned change_buffering:1;