Upstream: different header lists for cached and uncached requests.

The upstream modules remove and alter a number of client headers
before sending the request to upstream.  This set of headers is
smaller or even empty when cache is disabled.

It's still possible that a request in a cache-enabled location is
uncached, for example, if cache entry counter is below min_uses.
In this case it's better to alter a smaller set of headers and
pass more client headers to backend unchanged.  One of the benefits
is enabling server-side byte ranges in such requests.
This commit is contained in:
Roman Arutyunyan 2014-11-19 17:33:23 +03:00
parent bcf6b11a34
commit 8898c97dbd
4 changed files with 136 additions and 107 deletions

View File

@ -25,6 +25,9 @@ typedef struct {
ngx_str_t index;
ngx_http_fastcgi_params_t params;
#if (NGX_HTTP_CACHE)
ngx_http_fastcgi_params_t params_cache;
#endif
ngx_array_t *params_source;
ngx_array_t *catch_stderr;
@ -156,7 +159,8 @@ static void *ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_http_fastcgi_init_params(ngx_conf_t *cf,
ngx_http_fastcgi_loc_conf_t *conf, ngx_http_fastcgi_params_t *params);
ngx_http_fastcgi_loc_conf_t *conf, ngx_http_fastcgi_params_t *params,
ngx_keyval_t *default_params);
static ngx_int_t ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
@ -795,7 +799,11 @@ ngx_http_fastcgi_create_request(ngx_http_request_t *r)
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
#if (NGX_HTTP_CACHE)
params = r->upstream->cacheable ? &flcf->params_cache : &flcf->params;
#else
params = &flcf->params;
#endif
if (params->lengths) {
ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
@ -2420,6 +2428,7 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_http_fastcgi_loc_conf_t *conf = child;
size_t size;
ngx_int_t rc;
ngx_hash_init_t hash;
ngx_http_core_loc_conf_t *clcf;
@ -2712,19 +2721,29 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
#endif
if (conf->params_source == NULL) {
conf->params = prev->params;
#if (NGX_HTTP_CACHE)
conf->params_cache = prev->params_cache;
#endif
conf->params_source = prev->params_source;
}
rc = ngx_http_fastcgi_init_params(cf, conf, &conf->params, NULL);
if (rc != NGX_OK) {
return NGX_CONF_ERROR;
}
#if (NGX_HTTP_CACHE)
if ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
#endif
{
conf->params = prev->params;
if (conf->upstream.cache) {
rc = ngx_http_fastcgi_init_params(cf, conf, &conf->params_cache,
ngx_http_fastcgi_cache_headers);
if (rc != NGX_OK) {
return NGX_CONF_ERROR;
}
}
if (ngx_http_fastcgi_init_params(cf, conf, &conf->params) != NGX_OK) {
return NGX_CONF_ERROR;
}
#endif
return NGX_CONF_OK;
}
@ -2732,19 +2751,17 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
static ngx_int_t
ngx_http_fastcgi_init_params(ngx_conf_t *cf, ngx_http_fastcgi_loc_conf_t *conf,
ngx_http_fastcgi_params_t *params)
ngx_http_fastcgi_params_t *params, ngx_keyval_t *default_params)
{
u_char *p;
size_t size;
uintptr_t *code;
ngx_uint_t i, nsrc;
ngx_array_t headers_names;
#if (NGX_HTTP_CACHE)
ngx_array_t params_merged;
#endif
ngx_array_t headers_names, params_merged;
ngx_keyval_t *h;
ngx_hash_key_t *hk;
ngx_hash_init_t hash;
ngx_http_upstream_param_t *src;
ngx_http_upstream_param_t *src, *s;
ngx_http_script_compile_t sc;
ngx_http_script_copy_code_t *copy;
@ -2752,12 +2769,7 @@ ngx_http_fastcgi_init_params(ngx_conf_t *cf, ngx_http_fastcgi_loc_conf_t *conf,
return NGX_OK;
}
if (conf->params_source == NULL
#if (NGX_HTTP_CACHE)
&& (conf->upstream.cache == NULL)
#endif
)
{
if (conf->params_source == NULL && default_params == NULL) {
params->hash.buckets = (void *) 1;
return NGX_OK;
}
@ -2787,12 +2799,7 @@ ngx_http_fastcgi_init_params(ngx_conf_t *cf, ngx_http_fastcgi_loc_conf_t *conf,
nsrc = 0;
}
#if (NGX_HTTP_CACHE)
if (conf->upstream.cache) {
ngx_keyval_t *h;
ngx_http_upstream_param_t *s;
if (default_params) {
if (ngx_array_init(&params_merged, cf->temp_pool, 4,
sizeof(ngx_http_upstream_param_t))
!= NGX_OK)
@ -2810,7 +2817,7 @@ ngx_http_fastcgi_init_params(ngx_conf_t *cf, ngx_http_fastcgi_loc_conf_t *conf,
*s = src[i];
}
h = ngx_http_fastcgi_cache_headers;
h = default_params;
while (h->key.len) {
@ -2841,8 +2848,6 @@ ngx_http_fastcgi_init_params(ngx_conf_t *cf, ngx_http_fastcgi_loc_conf_t *conf,
nsrc = params_merged.nelts;
}
#endif
for (i = 0; i < nsrc; i++) {
if (src[i].key.len > sizeof("HTTP_") - 1

View File

@ -55,6 +55,9 @@ typedef struct {
ngx_array_t *body_set;
ngx_http_proxy_headers_t headers;
#if (NGX_HTTP_CACHE)
ngx_http_proxy_headers_t headers_cache;
#endif
ngx_array_t *headers_source;
ngx_array_t *proxy_lengths;
@ -153,7 +156,8 @@ static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_http_proxy_init_headers(ngx_conf_t *cf,
ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_headers_t *headers);
ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_headers_t *headers,
ngx_keyval_t *default_headers);
static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
@ -1095,7 +1099,11 @@ ngx_http_proxy_create_request(ngx_http_request_t *r)
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
#if (NGX_HTTP_CACHE)
headers = u->cacheable ? &plcf->headers_cache : &plcf->headers;
#else
headers = &plcf->headers;
#endif
if (u->method.len) {
/* HEAD was changed to GET to cache response */
@ -2515,6 +2523,9 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
* conf->headers.lengths = NULL;
* conf->headers.values = NULL;
* conf->headers.hash = { NULL, 0 };
* conf->headers_cache.lengths = NULL;
* conf->headers_cache.values = NULL;
* conf->headers_cache.hash = { NULL, 0 };
* conf->body_set_len = NULL;
* conf->body_set = NULL;
* conf->body_source = { 0, NULL };
@ -2606,6 +2617,7 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
u_char *p;
size_t size;
ngx_int_t rc;
ngx_hash_init_t hash;
ngx_http_core_loc_conf_t *clcf;
ngx_http_proxy_rewrite_t *pr;
@ -3028,26 +3040,37 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
if (conf->headers_source == NULL) {
conf->headers = prev->headers;
#if (NGX_HTTP_CACHE)
conf->headers_cache = prev->headers_cache;
#endif
conf->headers_source = prev->headers_source;
}
#if (NGX_HTTP_CACHE)
if ((conf->upstream.cache == NULL) != (prev->upstream.cache == NULL)) {
conf->headers.hash.buckets = NULL;
}
#endif
if (ngx_http_proxy_init_headers(cf, conf, &conf->headers) != NGX_OK) {
rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers,
ngx_http_proxy_headers);
if (rc != NGX_OK) {
return NGX_CONF_ERROR;
}
#if (NGX_HTTP_CACHE)
if (conf->upstream.cache) {
rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers_cache,
ngx_http_proxy_cache_headers);
if (rc != NGX_OK) {
return NGX_CONF_ERROR;
}
}
#endif
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_proxy_init_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
ngx_http_proxy_headers_t *headers)
ngx_http_proxy_headers_t *headers, ngx_keyval_t *default_headers)
{
u_char *p;
size_t size;
@ -3094,17 +3117,6 @@ ngx_http_proxy_init_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
return NGX_ERROR;
}
#if (NGX_HTTP_CACHE)
h = conf->upstream.cache ? ngx_http_proxy_cache_headers:
ngx_http_proxy_headers;
#else
h = ngx_http_proxy_headers;
#endif
src = conf->headers_source->elts;
for (i = 0; i < conf->headers_source->nelts; i++) {
@ -3116,6 +3128,8 @@ ngx_http_proxy_init_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
*s = src[i];
}
h = default_headers;
while (h->key.len) {
src = headers_merged.elts;

View File

@ -24,6 +24,9 @@ typedef struct {
ngx_http_upstream_conf_t upstream;
ngx_http_scgi_params_t params;
#if (NGX_HTTP_CACHE)
ngx_http_scgi_params_t params_cache;
#endif
ngx_array_t *params_source;
ngx_array_t *scgi_lengths;
@ -48,7 +51,8 @@ static void *ngx_http_scgi_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static ngx_int_t ngx_http_scgi_init_params(ngx_conf_t *cf,
ngx_http_scgi_loc_conf_t *conf, ngx_http_scgi_params_t *params);
ngx_http_scgi_loc_conf_t *conf, ngx_http_scgi_params_t *params,
ngx_keyval_t *default_params);
static char *ngx_http_scgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_scgi_store(ngx_conf_t *cf, ngx_command_t *cmd,
@ -608,7 +612,11 @@ ngx_http_scgi_create_request(ngx_http_request_t *r)
scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);
#if (NGX_HTTP_CACHE)
params = r->upstream->cacheable ? &scf->params_cache : &scf->params;
#else
params = &scf->params;
#endif
if (params->lengths) {
ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
@ -1174,6 +1182,7 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_http_scgi_loc_conf_t *conf = child;
size_t size;
ngx_int_t rc;
ngx_hash_init_t hash;
ngx_http_core_loc_conf_t *clcf;
@ -1451,19 +1460,29 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
}
if (conf->params_source == NULL) {
conf->params = prev->params;
#if (NGX_HTTP_CACHE)
conf->params_cache = prev->params_cache;
#endif
conf->params_source = prev->params_source;
}
rc = ngx_http_scgi_init_params(cf, conf, &conf->params, NULL);
if (rc != NGX_OK) {
return NGX_CONF_ERROR;
}
#if (NGX_HTTP_CACHE)
if ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
#endif
{
conf->params = prev->params;
if (conf->upstream.cache) {
rc = ngx_http_scgi_init_params(cf, conf, &conf->params_cache,
ngx_http_scgi_cache_headers);
if (rc != NGX_OK) {
return NGX_CONF_ERROR;
}
}
if (ngx_http_scgi_init_params(cf, conf, &conf->params) != NGX_OK) {
return NGX_CONF_ERROR;
}
#endif
return NGX_CONF_OK;
}
@ -1471,19 +1490,17 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
static ngx_int_t
ngx_http_scgi_init_params(ngx_conf_t *cf, ngx_http_scgi_loc_conf_t *conf,
ngx_http_scgi_params_t *params)
ngx_http_scgi_params_t *params, ngx_keyval_t *default_params)
{
u_char *p;
size_t size;
uintptr_t *code;
ngx_uint_t i, nsrc;
ngx_array_t headers_names;
#if (NGX_HTTP_CACHE)
ngx_array_t params_merged;
#endif
ngx_array_t headers_names, params_merged;
ngx_keyval_t *h;
ngx_hash_key_t *hk;
ngx_hash_init_t hash;
ngx_http_upstream_param_t *src;
ngx_http_upstream_param_t *src, *s;
ngx_http_script_compile_t sc;
ngx_http_script_copy_code_t *copy;
@ -1491,12 +1508,7 @@ ngx_http_scgi_init_params(ngx_conf_t *cf, ngx_http_scgi_loc_conf_t *conf,
return NGX_OK;
}
if (conf->params_source == NULL
#if (NGX_HTTP_CACHE)
&& (conf->upstream.cache == NULL)
#endif
)
{
if (conf->params_source == NULL && default_params == NULL) {
params->hash.buckets = (void *) 1;
return NGX_OK;
}
@ -1526,12 +1538,7 @@ ngx_http_scgi_init_params(ngx_conf_t *cf, ngx_http_scgi_loc_conf_t *conf,
nsrc = 0;
}
#if (NGX_HTTP_CACHE)
if (conf->upstream.cache) {
ngx_keyval_t *h;
ngx_http_upstream_param_t *s;
if (default_params) {
if (ngx_array_init(&params_merged, cf->temp_pool, 4,
sizeof(ngx_http_upstream_param_t))
!= NGX_OK)
@ -1549,7 +1556,7 @@ ngx_http_scgi_init_params(ngx_conf_t *cf, ngx_http_scgi_loc_conf_t *conf,
*s = src[i];
}
h = ngx_http_scgi_cache_headers;
h = default_params;
while (h->key.len) {
@ -1580,8 +1587,6 @@ ngx_http_scgi_init_params(ngx_conf_t *cf, ngx_http_scgi_loc_conf_t *conf,
nsrc = params_merged.nelts;
}
#endif
for (i = 0; i < nsrc; i++) {
if (src[i].key.len > sizeof("HTTP_") - 1

View File

@ -25,6 +25,9 @@ typedef struct {
ngx_http_upstream_conf_t upstream;
ngx_http_uwsgi_params_t params;
#if (NGX_HTTP_CACHE)
ngx_http_uwsgi_params_t params_cache;
#endif
ngx_array_t *params_source;
ngx_array_t *uwsgi_lengths;
@ -67,7 +70,8 @@ static void *ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static ngx_int_t ngx_http_uwsgi_init_params(ngx_conf_t *cf,
ngx_http_uwsgi_loc_conf_t *conf, ngx_http_uwsgi_params_t *params);
ngx_http_uwsgi_loc_conf_t *conf, ngx_http_uwsgi_params_t *params,
ngx_keyval_t *default_params);
static char *ngx_http_uwsgi_pass(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
@ -795,7 +799,11 @@ ngx_http_uwsgi_create_request(ngx_http_request_t *r)
uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
#if (NGX_HTTP_CACHE)
params = r->upstream->cacheable ? &uwcf->params_cache : &uwcf->params;
#else
params = &uwcf->params;
#endif
if (params->lengths) {
ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
@ -1388,6 +1396,7 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_http_uwsgi_loc_conf_t *conf = child;
size_t size;
ngx_int_t rc;
ngx_hash_init_t hash;
ngx_http_core_loc_conf_t *clcf;
@ -1713,19 +1722,29 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_uint_value(conf->modifier2, prev->modifier2, 0);
if (conf->params_source == NULL) {
conf->params = prev->params;
#if (NGX_HTTP_CACHE)
conf->params_cache = prev->params_cache;
#endif
conf->params_source = prev->params_source;
}
rc = ngx_http_uwsgi_init_params(cf, conf, &conf->params, NULL);
if (rc != NGX_OK) {
return NGX_CONF_ERROR;
}
#if (NGX_HTTP_CACHE)
if ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
#endif
{
conf->params = prev->params;
if (conf->upstream.cache) {
rc = ngx_http_uwsgi_init_params(cf, conf, &conf->params_cache,
ngx_http_uwsgi_cache_headers);
if (rc != NGX_OK) {
return NGX_CONF_ERROR;
}
}
if (ngx_http_uwsgi_init_params(cf, conf, &conf->params) != NGX_OK) {
return NGX_CONF_ERROR;
}
#endif
return NGX_CONF_OK;
}
@ -1733,19 +1752,17 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
static ngx_int_t
ngx_http_uwsgi_init_params(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *conf,
ngx_http_uwsgi_params_t *params)
ngx_http_uwsgi_params_t *params, ngx_keyval_t *default_params)
{
u_char *p;
size_t size;
uintptr_t *code;
ngx_uint_t i, nsrc;
ngx_array_t headers_names;
#if (NGX_HTTP_CACHE)
ngx_array_t params_merged;
#endif
ngx_array_t headers_names, params_merged;
ngx_keyval_t *h;
ngx_hash_key_t *hk;
ngx_hash_init_t hash;
ngx_http_upstream_param_t *src;
ngx_http_upstream_param_t *src, *s;
ngx_http_script_compile_t sc;
ngx_http_script_copy_code_t *copy;
@ -1753,12 +1770,7 @@ ngx_http_uwsgi_init_params(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *conf,
return NGX_OK;
}
if (conf->params_source == NULL
#if (NGX_HTTP_CACHE)
&& (conf->upstream.cache == NULL)
#endif
)
{
if (conf->params_source == NULL && default_params == NULL) {
params->hash.buckets = (void *) 1;
return NGX_OK;
}
@ -1788,12 +1800,7 @@ ngx_http_uwsgi_init_params(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *conf,
nsrc = 0;
}
#if (NGX_HTTP_CACHE)
if (conf->upstream.cache) {
ngx_keyval_t *h;
ngx_http_upstream_param_t *s;
if (default_params) {
if (ngx_array_init(&params_merged, cf->temp_pool, 4,
sizeof(ngx_http_upstream_param_t))
!= NGX_OK)
@ -1811,7 +1818,7 @@ ngx_http_uwsgi_init_params(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *conf,
*s = src[i];
}
h = ngx_http_uwsgi_cache_headers;
h = default_params;
while (h->key.len) {
@ -1842,8 +1849,6 @@ ngx_http_uwsgi_init_params(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *conf,
nsrc = params_merged.nelts;
}
#endif
for (i = 0; i < nsrc; i++) {
if (src[i].key.len > sizeof("HTTP_") - 1