comparison src/http/modules/ngx_http_proxy_module.c @ 5909:8d0cf26ce071

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.
author Roman Arutyunyan <arut@nginx.com>
date Wed, 19 Nov 2014 17:33:23 +0300
parents f8e80f8c7fc7
children 29fa5023bd6f
comparison
equal deleted inserted replaced
5908:f8e80f8c7fc7 5909:8d0cf26ce071
53 ngx_array_t *body_flushes; 53 ngx_array_t *body_flushes;
54 ngx_array_t *body_set_len; 54 ngx_array_t *body_set_len;
55 ngx_array_t *body_set; 55 ngx_array_t *body_set;
56 56
57 ngx_http_proxy_headers_t headers; 57 ngx_http_proxy_headers_t headers;
58 #if (NGX_HTTP_CACHE)
59 ngx_http_proxy_headers_t headers_cache;
60 #endif
58 ngx_array_t *headers_source; 61 ngx_array_t *headers_source;
59 62
60 ngx_array_t *proxy_lengths; 63 ngx_array_t *proxy_lengths;
61 ngx_array_t *proxy_values; 64 ngx_array_t *proxy_values;
62 65
151 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf); 154 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
152 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf); 155 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
153 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, 156 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
154 void *parent, void *child); 157 void *parent, void *child);
155 static ngx_int_t ngx_http_proxy_init_headers(ngx_conf_t *cf, 158 static ngx_int_t ngx_http_proxy_init_headers(ngx_conf_t *cf,
156 ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_headers_t *headers); 159 ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_headers_t *headers,
160 ngx_keyval_t *default_headers);
157 161
158 static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, 162 static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
159 void *conf); 163 void *conf);
160 static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, 164 static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
161 void *conf); 165 void *conf);
1093 1097
1094 u = r->upstream; 1098 u = r->upstream;
1095 1099
1096 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); 1100 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
1097 1101
1102 #if (NGX_HTTP_CACHE)
1103 headers = u->cacheable ? &plcf->headers_cache : &plcf->headers;
1104 #else
1098 headers = &plcf->headers; 1105 headers = &plcf->headers;
1106 #endif
1099 1107
1100 if (u->method.len) { 1108 if (u->method.len) {
1101 /* HEAD was changed to GET to cache response */ 1109 /* HEAD was changed to GET to cache response */
1102 method = u->method; 1110 method = u->method;
1103 method.len++; 1111 method.len++;
2513 * conf->method = { 0, NULL }; 2521 * conf->method = { 0, NULL };
2514 * conf->headers_source = NULL; 2522 * conf->headers_source = NULL;
2515 * conf->headers.lengths = NULL; 2523 * conf->headers.lengths = NULL;
2516 * conf->headers.values = NULL; 2524 * conf->headers.values = NULL;
2517 * conf->headers.hash = { NULL, 0 }; 2525 * conf->headers.hash = { NULL, 0 };
2526 * conf->headers_cache.lengths = NULL;
2527 * conf->headers_cache.values = NULL;
2528 * conf->headers_cache.hash = { NULL, 0 };
2518 * conf->body_set_len = NULL; 2529 * conf->body_set_len = NULL;
2519 * conf->body_set = NULL; 2530 * conf->body_set = NULL;
2520 * conf->body_source = { 0, NULL }; 2531 * conf->body_source = { 0, NULL };
2521 * conf->redirects = NULL; 2532 * conf->redirects = NULL;
2522 * conf->ssl = 0; 2533 * conf->ssl = 0;
2604 ngx_http_proxy_loc_conf_t *prev = parent; 2615 ngx_http_proxy_loc_conf_t *prev = parent;
2605 ngx_http_proxy_loc_conf_t *conf = child; 2616 ngx_http_proxy_loc_conf_t *conf = child;
2606 2617
2607 u_char *p; 2618 u_char *p;
2608 size_t size; 2619 size_t size;
2620 ngx_int_t rc;
2609 ngx_hash_init_t hash; 2621 ngx_hash_init_t hash;
2610 ngx_http_core_loc_conf_t *clcf; 2622 ngx_http_core_loc_conf_t *clcf;
2611 ngx_http_proxy_rewrite_t *pr; 2623 ngx_http_proxy_rewrite_t *pr;
2612 ngx_http_script_compile_t sc; 2624 ngx_http_script_compile_t sc;
2613 2625
3026 } 3038 }
3027 } 3039 }
3028 3040
3029 if (conf->headers_source == NULL) { 3041 if (conf->headers_source == NULL) {
3030 conf->headers = prev->headers; 3042 conf->headers = prev->headers;
3043 #if (NGX_HTTP_CACHE)
3044 conf->headers_cache = prev->headers_cache;
3045 #endif
3031 conf->headers_source = prev->headers_source; 3046 conf->headers_source = prev->headers_source;
3032 } 3047 }
3033 3048
3049 rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers,
3050 ngx_http_proxy_headers);
3051 if (rc != NGX_OK) {
3052 return NGX_CONF_ERROR;
3053 }
3054
3034 #if (NGX_HTTP_CACHE) 3055 #if (NGX_HTTP_CACHE)
3035 if ((conf->upstream.cache == NULL) != (prev->upstream.cache == NULL)) { 3056
3036 conf->headers.hash.buckets = NULL; 3057 if (conf->upstream.cache) {
3037 } 3058 rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers_cache,
3059 ngx_http_proxy_cache_headers);
3060 if (rc != NGX_OK) {
3061 return NGX_CONF_ERROR;
3062 }
3063 }
3064
3038 #endif 3065 #endif
3039
3040 if (ngx_http_proxy_init_headers(cf, conf, &conf->headers) != NGX_OK) {
3041 return NGX_CONF_ERROR;
3042 }
3043 3066
3044 return NGX_CONF_OK; 3067 return NGX_CONF_OK;
3045 } 3068 }
3046 3069
3047 3070
3048 static ngx_int_t 3071 static ngx_int_t
3049 ngx_http_proxy_init_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf, 3072 ngx_http_proxy_init_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
3050 ngx_http_proxy_headers_t *headers) 3073 ngx_http_proxy_headers_t *headers, ngx_keyval_t *default_headers)
3051 { 3074 {
3052 u_char *p; 3075 u_char *p;
3053 size_t size; 3076 size_t size;
3054 uintptr_t *code; 3077 uintptr_t *code;
3055 ngx_uint_t i; 3078 ngx_uint_t i;
3092 headers->values = ngx_array_create(cf->pool, 512, 1); 3115 headers->values = ngx_array_create(cf->pool, 512, 1);
3093 if (headers->values == NULL) { 3116 if (headers->values == NULL) {
3094 return NGX_ERROR; 3117 return NGX_ERROR;
3095 } 3118 }
3096 3119
3097
3098 #if (NGX_HTTP_CACHE)
3099
3100 h = conf->upstream.cache ? ngx_http_proxy_cache_headers:
3101 ngx_http_proxy_headers;
3102 #else
3103
3104 h = ngx_http_proxy_headers;
3105
3106 #endif
3107
3108 src = conf->headers_source->elts; 3120 src = conf->headers_source->elts;
3109 for (i = 0; i < conf->headers_source->nelts; i++) { 3121 for (i = 0; i < conf->headers_source->nelts; i++) {
3110 3122
3111 s = ngx_array_push(&headers_merged); 3123 s = ngx_array_push(&headers_merged);
3112 if (s == NULL) { 3124 if (s == NULL) {
3113 return NGX_ERROR; 3125 return NGX_ERROR;
3114 } 3126 }
3115 3127
3116 *s = src[i]; 3128 *s = src[i];
3117 } 3129 }
3130
3131 h = default_headers;
3118 3132
3119 while (h->key.len) { 3133 while (h->key.len) {
3120 3134
3121 src = headers_merged.elts; 3135 src = headers_merged.elts;
3122 for (i = 0; i < headers_merged.nelts; i++) { 3136 for (i = 0; i < headers_merged.nelts; i++) {