comparison src/http/modules/ngx_http_fastcgi_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 5b9f711dc819
comparison
equal deleted inserted replaced
5908:f8e80f8c7fc7 5909:8d0cf26ce071
23 ngx_http_upstream_conf_t upstream; 23 ngx_http_upstream_conf_t upstream;
24 24
25 ngx_str_t index; 25 ngx_str_t index;
26 26
27 ngx_http_fastcgi_params_t params; 27 ngx_http_fastcgi_params_t params;
28 #if (NGX_HTTP_CACHE)
29 ngx_http_fastcgi_params_t params_cache;
30 #endif
28 31
29 ngx_array_t *params_source; 32 ngx_array_t *params_source;
30 ngx_array_t *catch_stderr; 33 ngx_array_t *catch_stderr;
31 34
32 ngx_array_t *fastcgi_lengths; 35 ngx_array_t *fastcgi_lengths;
154 static ngx_int_t ngx_http_fastcgi_add_variables(ngx_conf_t *cf); 157 static ngx_int_t ngx_http_fastcgi_add_variables(ngx_conf_t *cf);
155 static void *ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf); 158 static void *ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf);
156 static char *ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, 159 static char *ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf,
157 void *parent, void *child); 160 void *parent, void *child);
158 static ngx_int_t ngx_http_fastcgi_init_params(ngx_conf_t *cf, 161 static ngx_int_t ngx_http_fastcgi_init_params(ngx_conf_t *cf,
159 ngx_http_fastcgi_loc_conf_t *conf, ngx_http_fastcgi_params_t *params); 162 ngx_http_fastcgi_loc_conf_t *conf, ngx_http_fastcgi_params_t *params,
163 ngx_keyval_t *default_params);
160 164
161 static ngx_int_t ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r, 165 static ngx_int_t ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r,
162 ngx_http_variable_value_t *v, uintptr_t data); 166 ngx_http_variable_value_t *v, uintptr_t data);
163 static ngx_int_t ngx_http_fastcgi_path_info_variable(ngx_http_request_t *r, 167 static ngx_int_t ngx_http_fastcgi_path_info_variable(ngx_http_request_t *r,
164 ngx_http_variable_value_t *v, uintptr_t data); 168 ngx_http_variable_value_t *v, uintptr_t data);
793 header_params = 0; 797 header_params = 0;
794 ignored = NULL; 798 ignored = NULL;
795 799
796 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module); 800 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
797 801
802 #if (NGX_HTTP_CACHE)
803 params = r->upstream->cacheable ? &flcf->params_cache : &flcf->params;
804 #else
798 params = &flcf->params; 805 params = &flcf->params;
806 #endif
799 807
800 if (params->lengths) { 808 if (params->lengths) {
801 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); 809 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
802 810
803 ngx_http_script_flush_no_cacheable_variables(r, params->flushes); 811 ngx_http_script_flush_no_cacheable_variables(r, params->flushes);
2418 { 2426 {
2419 ngx_http_fastcgi_loc_conf_t *prev = parent; 2427 ngx_http_fastcgi_loc_conf_t *prev = parent;
2420 ngx_http_fastcgi_loc_conf_t *conf = child; 2428 ngx_http_fastcgi_loc_conf_t *conf = child;
2421 2429
2422 size_t size; 2430 size_t size;
2431 ngx_int_t rc;
2423 ngx_hash_init_t hash; 2432 ngx_hash_init_t hash;
2424 ngx_http_core_loc_conf_t *clcf; 2433 ngx_http_core_loc_conf_t *clcf;
2425 2434
2426 if (conf->upstream.store != 0) { 2435 if (conf->upstream.store != 0) {
2427 ngx_conf_merge_value(conf->upstream.store, 2436 ngx_conf_merge_value(conf->upstream.store,
2710 conf->split_name = prev->split_name; 2719 conf->split_name = prev->split_name;
2711 } 2720 }
2712 #endif 2721 #endif
2713 2722
2714 if (conf->params_source == NULL) { 2723 if (conf->params_source == NULL) {
2724 conf->params = prev->params;
2725 #if (NGX_HTTP_CACHE)
2726 conf->params_cache = prev->params_cache;
2727 #endif
2715 conf->params_source = prev->params_source; 2728 conf->params_source = prev->params_source;
2729 }
2730
2731 rc = ngx_http_fastcgi_init_params(cf, conf, &conf->params, NULL);
2732 if (rc != NGX_OK) {
2733 return NGX_CONF_ERROR;
2734 }
2716 2735
2717 #if (NGX_HTTP_CACHE) 2736 #if (NGX_HTTP_CACHE)
2718 if ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL)) 2737
2738 if (conf->upstream.cache) {
2739 rc = ngx_http_fastcgi_init_params(cf, conf, &conf->params_cache,
2740 ngx_http_fastcgi_cache_headers);
2741 if (rc != NGX_OK) {
2742 return NGX_CONF_ERROR;
2743 }
2744 }
2745
2719 #endif 2746 #endif
2720 {
2721 conf->params = prev->params;
2722 }
2723 }
2724
2725 if (ngx_http_fastcgi_init_params(cf, conf, &conf->params) != NGX_OK) {
2726 return NGX_CONF_ERROR;
2727 }
2728 2747
2729 return NGX_CONF_OK; 2748 return NGX_CONF_OK;
2730 } 2749 }
2731 2750
2732 2751
2733 static ngx_int_t 2752 static ngx_int_t
2734 ngx_http_fastcgi_init_params(ngx_conf_t *cf, ngx_http_fastcgi_loc_conf_t *conf, 2753 ngx_http_fastcgi_init_params(ngx_conf_t *cf, ngx_http_fastcgi_loc_conf_t *conf,
2735 ngx_http_fastcgi_params_t *params) 2754 ngx_http_fastcgi_params_t *params, ngx_keyval_t *default_params)
2736 { 2755 {
2737 u_char *p; 2756 u_char *p;
2738 size_t size; 2757 size_t size;
2739 uintptr_t *code; 2758 uintptr_t *code;
2740 ngx_uint_t i, nsrc; 2759 ngx_uint_t i, nsrc;
2741 ngx_array_t headers_names; 2760 ngx_array_t headers_names, params_merged;
2742 #if (NGX_HTTP_CACHE) 2761 ngx_keyval_t *h;
2743 ngx_array_t params_merged;
2744 #endif
2745 ngx_hash_key_t *hk; 2762 ngx_hash_key_t *hk;
2746 ngx_hash_init_t hash; 2763 ngx_hash_init_t hash;
2747 ngx_http_upstream_param_t *src; 2764 ngx_http_upstream_param_t *src, *s;
2748 ngx_http_script_compile_t sc; 2765 ngx_http_script_compile_t sc;
2749 ngx_http_script_copy_code_t *copy; 2766 ngx_http_script_copy_code_t *copy;
2750 2767
2751 if (params->hash.buckets) { 2768 if (params->hash.buckets) {
2752 return NGX_OK; 2769 return NGX_OK;
2753 } 2770 }
2754 2771
2755 if (conf->params_source == NULL 2772 if (conf->params_source == NULL && default_params == NULL) {
2756 #if (NGX_HTTP_CACHE)
2757 && (conf->upstream.cache == NULL)
2758 #endif
2759 )
2760 {
2761 params->hash.buckets = (void *) 1; 2773 params->hash.buckets = (void *) 1;
2762 return NGX_OK; 2774 return NGX_OK;
2763 } 2775 }
2764 2776
2765 params->lengths = ngx_array_create(cf->pool, 64, 1); 2777 params->lengths = ngx_array_create(cf->pool, 64, 1);
2785 } else { 2797 } else {
2786 src = NULL; 2798 src = NULL;
2787 nsrc = 0; 2799 nsrc = 0;
2788 } 2800 }
2789 2801
2790 #if (NGX_HTTP_CACHE) 2802 if (default_params) {
2791
2792 if (conf->upstream.cache) {
2793 ngx_keyval_t *h;
2794 ngx_http_upstream_param_t *s;
2795
2796 if (ngx_array_init(&params_merged, cf->temp_pool, 4, 2803 if (ngx_array_init(&params_merged, cf->temp_pool, 4,
2797 sizeof(ngx_http_upstream_param_t)) 2804 sizeof(ngx_http_upstream_param_t))
2798 != NGX_OK) 2805 != NGX_OK)
2799 { 2806 {
2800 return NGX_ERROR; 2807 return NGX_ERROR;
2808 } 2815 }
2809 2816
2810 *s = src[i]; 2817 *s = src[i];
2811 } 2818 }
2812 2819
2813 h = ngx_http_fastcgi_cache_headers; 2820 h = default_params;
2814 2821
2815 while (h->key.len) { 2822 while (h->key.len) {
2816 2823
2817 src = params_merged.elts; 2824 src = params_merged.elts;
2818 nsrc = params_merged.nelts; 2825 nsrc = params_merged.nelts;
2838 } 2845 }
2839 2846
2840 src = params_merged.elts; 2847 src = params_merged.elts;
2841 nsrc = params_merged.nelts; 2848 nsrc = params_merged.nelts;
2842 } 2849 }
2843
2844 #endif
2845 2850
2846 for (i = 0; i < nsrc; i++) { 2851 for (i = 0; i < nsrc; i++) {
2847 2852
2848 if (src[i].key.len > sizeof("HTTP_") - 1 2853 if (src[i].key.len > sizeof("HTTP_") - 1
2849 && ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0) 2854 && ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0)