Skip to content

Commit

Permalink
Upstream: do not unnecessarily create per-request upstreams.
Browse files Browse the repository at this point in the history
If proxy_pass (and friends) with variables evaluates an upstream
specified with literal address, nginx always created a per-request
upstream.

Now, if there's a matching upstream specified in the configuration
(either implicit or explicit), it will be used instead.
  • Loading branch information
mdocguard committed Oct 31, 2016
1 parent 3fae83a commit 149fda5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
34 changes: 17 additions & 17 deletions src/http/ngx_http_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,23 @@ ngx_http_upstream_init_request(ngx_http_request_t *r)

host = &u->resolved->host;

umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);

uscfp = umcf->upstreams.elts;

for (i = 0; i < umcf->upstreams.nelts; i++) {

uscf = uscfp[i];

if (uscf->host.len == host->len
&& ((uscf->port == 0 && u->resolved->no_port)
|| uscf->port == u->resolved->port)
&& ngx_strncasecmp(uscf->host.data, host->data, host->len) == 0)
{
goto found;
}
}

if (u->resolved->sockaddr) {

if (u->resolved->port == 0
Expand All @@ -679,23 +696,6 @@ ngx_http_upstream_init_request(ngx_http_request_t *r)
return;
}

umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);

uscfp = umcf->upstreams.elts;

for (i = 0; i < umcf->upstreams.nelts; i++) {

uscf = uscfp[i];

if (uscf->host.len == host->len
&& ((uscf->port == 0 && u->resolved->no_port)
|| uscf->port == u->resolved->port)
&& ngx_strncasecmp(uscf->host.data, host->data, host->len) == 0)
{
goto found;
}
}

if (u->resolved->port == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"no port in upstream \"%V\"", host);
Expand Down
34 changes: 17 additions & 17 deletions src/stream/ngx_stream_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,23 @@ ngx_stream_proxy_handler(ngx_stream_session_t *s)

host = &u->resolved->host;

umcf = ngx_stream_get_module_main_conf(s, ngx_stream_upstream_module);

uscfp = umcf->upstreams.elts;

for (i = 0; i < umcf->upstreams.nelts; i++) {

uscf = uscfp[i];

if (uscf->host.len == host->len
&& ((uscf->port == 0 && u->resolved->no_port)
|| uscf->port == u->resolved->port)
&& ngx_strncasecmp(uscf->host.data, host->data, host->len) == 0)
{
goto found;
}
}

if (u->resolved->sockaddr) {

if (u->resolved->port == 0
Expand All @@ -456,23 +473,6 @@ ngx_stream_proxy_handler(ngx_stream_session_t *s)
return;
}

umcf = ngx_stream_get_module_main_conf(s, ngx_stream_upstream_module);

uscfp = umcf->upstreams.elts;

for (i = 0; i < umcf->upstreams.nelts; i++) {

uscf = uscfp[i];

if (uscf->host.len == host->len
&& ((uscf->port == 0 && u->resolved->no_port)
|| uscf->port == u->resolved->port)
&& ngx_strncasecmp(uscf->host.data, host->data, host->len) == 0)
{
goto found;
}
}

if (u->resolved->port == 0) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"no port in upstream \"%V\"", host);
Expand Down

0 comments on commit 149fda5

Please sign in to comment.