Skip to content

Commit

Permalink
Merge pull request alibaba#257 from jinglong/bind
Browse files Browse the repository at this point in the history
update proxy_bind to support variable
  • Loading branch information
yaoweibin committed Jun 4, 2013
2 parents 96b7d2e + 055fab6 commit 9a45f4f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/http/modules/ngx_http_fastcgi_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,8 @@ ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf)
conf->upstream.buffering = NGX_CONF_UNSET;
conf->upstream.ignore_client_abort = NGX_CONF_UNSET;

conf->upstream.local = NGX_CONF_UNSET_PTR;

conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
Expand Down Expand Up @@ -2162,6 +2164,9 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->upstream.ignore_client_abort,
prev->upstream.ignore_client_abort, 0);

ngx_conf_merge_ptr_value(conf->upstream.local,
prev->upstream.local, NULL);

ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

Expand Down
4 changes: 4 additions & 0 deletions src/http/modules/ngx_http_memcached_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ ngx_http_memcached_create_loc_conf(ngx_conf_t *cf)
* conf->upstream.location = NULL;
*/

conf->upstream.local = NGX_CONF_UNSET_PTR;
conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
Expand Down Expand Up @@ -561,6 +562,9 @@ ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_http_memcached_loc_conf_t *prev = parent;
ngx_http_memcached_loc_conf_t *conf = child;

ngx_conf_merge_ptr_value(conf->upstream.local,
prev->upstream.local, NULL);

ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

Expand Down
5 changes: 5 additions & 0 deletions src/http/modules/ngx_http_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,8 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
conf->upstream.buffering = NGX_CONF_UNSET;
conf->upstream.ignore_client_abort = NGX_CONF_UNSET;

conf->upstream.local = NGX_CONF_UNSET_PTR;

conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
Expand Down Expand Up @@ -2712,6 +2714,9 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->upstream.ignore_client_abort,
prev->upstream.ignore_client_abort, 0);

ngx_conf_merge_ptr_value(conf->upstream.local,
prev->upstream.local, NULL);

ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

Expand Down
5 changes: 5 additions & 0 deletions src/http/modules/ngx_http_scgi_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,8 @@ ngx_http_scgi_create_loc_conf(ngx_conf_t *cf)
conf->upstream.buffering = NGX_CONF_UNSET;
conf->upstream.ignore_client_abort = NGX_CONF_UNSET;

conf->upstream.local = NGX_CONF_UNSET_PTR;

conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
Expand Down Expand Up @@ -1129,6 +1131,9 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->upstream.ignore_client_abort,
prev->upstream.ignore_client_abort, 0);

ngx_conf_merge_ptr_value(conf->upstream.local,
prev->upstream.local, NULL);

ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

Expand Down
5 changes: 5 additions & 0 deletions src/http/modules/ngx_http_uwsgi_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,8 @@ ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf)
conf->upstream.buffering = NGX_CONF_UNSET;
conf->upstream.ignore_client_abort = NGX_CONF_UNSET;

conf->upstream.local = NGX_CONF_UNSET_PTR;

conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
Expand Down Expand Up @@ -1172,6 +1174,9 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->upstream.ignore_client_abort,
prev->upstream.ignore_client_abort, 0);

ngx_conf_merge_ptr_value(conf->upstream.local,
prev->upstream.local, NULL);

ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

Expand Down
2 changes: 1 addition & 1 deletion src/http/ngx_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ typedef u_char *(*ngx_http_log_handler_pt)(ngx_http_request_t *r,

#include <ngx_http_variables.h>
#include <ngx_http_request.h>
#include <ngx_http_script.h>
#include <ngx_http_upstream.h>
#include <ngx_http_upstream_round_robin.h>
#include <ngx_http_config.h>
#include <ngx_http_busy_lock.h>
#include <ngx_http_script.h>
#include <ngx_http_core_module.h>

#if (NGX_HTTP_CACHE)
Expand Down
109 changes: 99 additions & 10 deletions src/http/ngx_http_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);

static ngx_addr_t *ngx_http_upstream_get_local(ngx_http_request_t *r,
ngx_http_upstream_local_t *local);

static void *ngx_http_upstream_create_main_conf(ngx_conf_t *cf);
static char *ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf);

Expand Down Expand Up @@ -507,7 +510,7 @@ ngx_http_upstream_init_request(ngx_http_request_t *r)
return;
}

u->peer.local = u->conf->local;
u->peer.local = ngx_http_upstream_get_local(r, u->conf->local);

clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

Expand Down Expand Up @@ -4494,24 +4497,63 @@ ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
{
char *p = conf;

ngx_int_t rc;
ngx_str_t *value;
ngx_addr_t **paddr;
ngx_int_t rc;
ngx_str_t *value;
ngx_http_complex_value_t cv;
ngx_http_upstream_local_t **plocal, *local;
ngx_http_compile_complex_value_t ccv;

paddr = (ngx_addr_t **) (p + cmd->offset);
plocal = (ngx_http_upstream_local_t **) (p + cmd->offset);

*paddr = ngx_palloc(cf->pool, sizeof(ngx_addr_t));
if (*paddr == NULL) {
return NGX_CONF_ERROR;
if (*plocal != NGX_CONF_UNSET_PTR) {
return "is duplicate";
}

value = cf->args->elts;

rc = ngx_parse_addr(cf->pool, *paddr, value[1].data, value[1].len);
if (ngx_strcmp(value[1].data, "off") == 0) {
*plocal = NULL;
return NGX_CONF_OK;
}

ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));

ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = &cv;

if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}

local = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_local_t));
if (local == NULL) {
return NGX_CONF_ERROR;
}

*plocal = local;

if (cv.lengths) {
local->value = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
if (local->value == NULL) {
return NGX_CONF_ERROR;
}

*local->value = cv;

return NGX_CONF_OK;
}

local->addr = ngx_palloc(cf->pool, sizeof(ngx_addr_t));
if (local->addr == NULL) {
return NGX_CONF_ERROR;
}

rc = ngx_parse_addr(cf->pool, local->addr, value[1].data, value[1].len);

switch (rc) {
case NGX_OK:
(*paddr)->name = value[1];
local->addr->name = value[1];
return NGX_CONF_OK;

case NGX_DECLINED:
Expand All @@ -4525,6 +4567,53 @@ ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
}


static ngx_addr_t *
ngx_http_upstream_get_local(ngx_http_request_t *r,
ngx_http_upstream_local_t *local)
{
ngx_int_t rc;
ngx_str_t val;
ngx_addr_t *addr;

if (local == NULL) {
return NULL;
}

if (local->value == NULL) {
return local->addr;
}

if (ngx_http_complex_value(r, local->value, &val) != NGX_OK) {
return NULL;
}

if (val.len == 0) {
return NULL;
}

addr = ngx_palloc(r->pool, sizeof(ngx_addr_t));
if (addr == NULL) {
return NULL;
}

rc = ngx_parse_addr(r->pool, addr, val.data, val.len);

switch (rc) {
case NGX_OK:
addr->name = val;
return addr;

case NGX_DECLINED:
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"invalid local address \"%V\"", &val);
/* fall through */

default:
return NULL;
}
}


char *
ngx_http_upstream_param_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
Expand Down
8 changes: 7 additions & 1 deletion src/http/ngx_http_upstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ struct ngx_http_upstream_srv_conf_s {
};


typedef struct {
ngx_addr_t *addr;
ngx_http_complex_value_t *value;
} ngx_http_upstream_local_t;


typedef struct {
ngx_http_upstream_srv_conf_t *upstream;

Expand Down Expand Up @@ -165,7 +171,7 @@ typedef struct {
ngx_array_t *hide_headers;
ngx_array_t *pass_headers;

ngx_addr_t *local;
ngx_http_upstream_local_t *local;

#if (NGX_HTTP_CACHE)
ngx_shm_zone_t *cache;
Expand Down

0 comments on commit 9a45f4f

Please sign in to comment.