comparison src/http/modules/ngx_http_proxy_module.c @ 5951:610832763648

Upstream: added variables support to proxy_cache and friends.
author Valentin Bartenev <vbart@nginx.com>
date Mon, 22 Dec 2014 12:59:09 +0300
parents 634da79665d3
children cf2f8d91cf09
comparison
equal deleted inserted replaced
5950:eaeecf00d5d7 5951:610832763648
6 6
7 7
8 #include <ngx_config.h> 8 #include <ngx_config.h>
9 #include <ngx_core.h> 9 #include <ngx_core.h>
10 #include <ngx_http.h> 10 #include <ngx_http.h>
11
12
13 typedef struct {
14 ngx_array_t caches; /* ngx_http_file_cache_t * */
15 } ngx_http_proxy_main_conf_t;
11 16
12 17
13 typedef struct ngx_http_proxy_rewrite_s ngx_http_proxy_rewrite_t; 18 typedef struct ngx_http_proxy_rewrite_s ngx_http_proxy_rewrite_t;
14 19
15 typedef ngx_int_t (*ngx_http_proxy_rewrite_pt)(ngx_http_request_t *r, 20 typedef ngx_int_t (*ngx_http_proxy_rewrite_pt)(ngx_http_request_t *r,
149 ngx_table_elt_t *h, u_char *value, ngx_array_t *rewrites); 154 ngx_table_elt_t *h, u_char *value, ngx_array_t *rewrites);
150 static ngx_int_t ngx_http_proxy_rewrite(ngx_http_request_t *r, 155 static ngx_int_t ngx_http_proxy_rewrite(ngx_http_request_t *r,
151 ngx_table_elt_t *h, size_t prefix, size_t len, ngx_str_t *replacement); 156 ngx_table_elt_t *h, size_t prefix, size_t len, ngx_str_t *replacement);
152 157
153 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf); 158 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
159 static void *ngx_http_proxy_create_main_conf(ngx_conf_t *cf);
154 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf); 160 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
155 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, 161 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
156 void *parent, void *child); 162 void *parent, void *child);
157 static ngx_int_t ngx_http_proxy_init_headers(ngx_conf_t *cf, 163 static ngx_int_t ngx_http_proxy_init_headers(ngx_conf_t *cf,
158 ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_headers_t *headers, 164 ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_headers_t *headers,
436 NULL }, 442 NULL },
437 443
438 { ngx_string("proxy_cache_path"), 444 { ngx_string("proxy_cache_path"),
439 NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE, 445 NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
440 ngx_http_file_cache_set_slot, 446 ngx_http_file_cache_set_slot,
441 0, 447 NGX_HTTP_MAIN_CONF_OFFSET,
442 0, 448 offsetof(ngx_http_proxy_main_conf_t, caches),
443 &ngx_http_proxy_module }, 449 &ngx_http_proxy_module },
444 450
445 { ngx_string("proxy_cache_bypass"), 451 { ngx_string("proxy_cache_bypass"),
446 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, 452 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
447 ngx_http_set_predicate_slot, 453 ngx_http_set_predicate_slot,
678 684
679 static ngx_http_module_t ngx_http_proxy_module_ctx = { 685 static ngx_http_module_t ngx_http_proxy_module_ctx = {
680 ngx_http_proxy_add_variables, /* preconfiguration */ 686 ngx_http_proxy_add_variables, /* preconfiguration */
681 NULL, /* postconfiguration */ 687 NULL, /* postconfiguration */
682 688
683 NULL, /* create main configuration */ 689 ngx_http_proxy_create_main_conf, /* create main configuration */
684 NULL, /* init main configuration */ 690 NULL, /* init main configuration */
685 691
686 NULL, /* create server configuration */ 692 NULL, /* create server configuration */
687 NULL, /* merge server configuration */ 693 NULL, /* merge server configuration */
688 694
790 796
791 797
792 static ngx_int_t 798 static ngx_int_t
793 ngx_http_proxy_handler(ngx_http_request_t *r) 799 ngx_http_proxy_handler(ngx_http_request_t *r)
794 { 800 {
795 ngx_int_t rc; 801 ngx_int_t rc;
796 ngx_http_upstream_t *u; 802 ngx_http_upstream_t *u;
797 ngx_http_proxy_ctx_t *ctx; 803 ngx_http_proxy_ctx_t *ctx;
798 ngx_http_proxy_loc_conf_t *plcf; 804 ngx_http_proxy_loc_conf_t *plcf;
805 #if (NGX_HTTP_CACHE)
806 ngx_http_proxy_main_conf_t *pmcf;
807 #endif
799 808
800 if (ngx_http_upstream_create(r) != NGX_OK) { 809 if (ngx_http_upstream_create(r) != NGX_OK) {
801 return NGX_HTTP_INTERNAL_SERVER_ERROR; 810 return NGX_HTTP_INTERNAL_SERVER_ERROR;
802 } 811 }
803 812
828 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module; 837 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
829 838
830 u->conf = &plcf->upstream; 839 u->conf = &plcf->upstream;
831 840
832 #if (NGX_HTTP_CACHE) 841 #if (NGX_HTTP_CACHE)
842 pmcf = ngx_http_get_module_main_conf(r, ngx_http_proxy_module);
843
844 u->caches = &pmcf->caches;
833 u->create_key = ngx_http_proxy_create_key; 845 u->create_key = ngx_http_proxy_create_key;
834 #endif 846 #endif
847
835 u->create_request = ngx_http_proxy_create_request; 848 u->create_request = ngx_http_proxy_create_request;
836 u->reinit_request = ngx_http_proxy_reinit_request; 849 u->reinit_request = ngx_http_proxy_reinit_request;
837 u->process_header = ngx_http_proxy_process_status_line; 850 u->process_header = ngx_http_proxy_process_status_line;
838 u->abort_request = ngx_http_proxy_abort_request; 851 u->abort_request = ngx_http_proxy_abort_request;
839 u->finalize_request = ngx_http_proxy_finalize_request; 852 u->finalize_request = ngx_http_proxy_finalize_request;
2488 var->get_handler = v->get_handler; 2501 var->get_handler = v->get_handler;
2489 var->data = v->data; 2502 var->data = v->data;
2490 } 2503 }
2491 2504
2492 return NGX_OK; 2505 return NGX_OK;
2506 }
2507
2508
2509 static void *
2510 ngx_http_proxy_create_main_conf(ngx_conf_t *cf)
2511 {
2512 ngx_http_proxy_main_conf_t *conf;
2513
2514 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_main_conf_t));
2515 if (conf == NULL) {
2516 return NULL;
2517 }
2518
2519 #if (NGX_HTTP_CACHE)
2520 if (ngx_array_init(&conf->caches, cf->pool, 4,
2521 sizeof(ngx_http_file_cache_t *))
2522 != NGX_OK)
2523 {
2524 return NULL;
2525 }
2526 #endif
2527
2528 return conf;
2493 } 2529 }
2494 2530
2495 2531
2496 static void * 2532 static void *
2497 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) 2533 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
2806 if (conf->upstream.cache == NGX_CONF_UNSET) { 2842 if (conf->upstream.cache == NGX_CONF_UNSET) {
2807 ngx_conf_merge_value(conf->upstream.cache, 2843 ngx_conf_merge_value(conf->upstream.cache,
2808 prev->upstream.cache, 0); 2844 prev->upstream.cache, 0);
2809 2845
2810 conf->upstream.cache_zone = prev->upstream.cache_zone; 2846 conf->upstream.cache_zone = prev->upstream.cache_zone;
2847 conf->upstream.cache_value = prev->upstream.cache_value;
2811 } 2848 }
2812 2849
2813 if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) { 2850 if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) {
2814 ngx_shm_zone_t *shm_zone; 2851 ngx_shm_zone_t *shm_zone;
2815 2852
3858 static char * 3895 static char *
3859 ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 3896 ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3860 { 3897 {
3861 ngx_http_proxy_loc_conf_t *plcf = conf; 3898 ngx_http_proxy_loc_conf_t *plcf = conf;
3862 3899
3863 ngx_str_t *value; 3900 ngx_str_t *value;
3901 ngx_http_complex_value_t cv;
3902 ngx_http_compile_complex_value_t ccv;
3864 3903
3865 value = cf->args->elts; 3904 value = cf->args->elts;
3866 3905
3867 if (plcf->upstream.cache != NGX_CONF_UNSET) { 3906 if (plcf->upstream.cache != NGX_CONF_UNSET) {
3868 return "is duplicate"; 3907 return "is duplicate";
3876 if (plcf->upstream.store > 0) { 3915 if (plcf->upstream.store > 0) {
3877 return "is incompatible with \"proxy_store\""; 3916 return "is incompatible with \"proxy_store\"";
3878 } 3917 }
3879 3918
3880 plcf->upstream.cache = 1; 3919 plcf->upstream.cache = 1;
3920
3921 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3922
3923 ccv.cf = cf;
3924 ccv.value = &value[1];
3925 ccv.complex_value = &cv;
3926
3927 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3928 return NGX_CONF_ERROR;
3929 }
3930
3931 if (cv.lengths != NULL) {
3932
3933 plcf->upstream.cache_value = ngx_palloc(cf->pool,
3934 sizeof(ngx_http_complex_value_t));
3935 if (plcf->upstream.cache_value == NULL) {
3936 return NGX_CONF_ERROR;
3937 }
3938
3939 *plcf->upstream.cache_value = cv;
3940
3941 return NGX_CONF_OK;
3942 }
3881 3943
3882 plcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0, 3944 plcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
3883 &ngx_http_proxy_module); 3945 &ngx_http_proxy_module);
3884 if (plcf->upstream.cache_zone == NULL) { 3946 if (plcf->upstream.cache_zone == NULL) {
3885 return NGX_CONF_ERROR; 3947 return NGX_CONF_ERROR;