The default server lookup is now done only once per connection.

Previously, it was done for every request in a connection.
This commit is contained in:
Valentin Bartenev 2013-02-27 16:53:01 +00:00
parent 67a68720b7
commit 508afb8cf5
3 changed files with 126 additions and 126 deletions

View File

@ -209,6 +209,23 @@ typedef struct {
typedef struct { typedef struct {
#if (NGX_PCRE)
ngx_http_regex_t *regex;
#endif
ngx_http_core_srv_conf_t *server; /* virtual name server conf */
ngx_str_t name;
} ngx_http_server_name_t;
typedef struct {
ngx_hash_combined_t names;
ngx_uint_t nregex;
ngx_http_server_name_t *regex;
} ngx_http_virtual_names_t;
struct ngx_http_addr_conf_s {
/* the default server configuration for this address:port */ /* the default server configuration for this address:port */
ngx_http_core_srv_conf_t *default_server; ngx_http_core_srv_conf_t *default_server;
@ -217,7 +234,7 @@ typedef struct {
#if (NGX_HTTP_SSL) #if (NGX_HTTP_SSL)
ngx_uint_t ssl; /* unsigned ssl:1; */ ngx_uint_t ssl; /* unsigned ssl:1; */
#endif #endif
} ngx_http_addr_conf_t; };
typedef struct { typedef struct {
@ -268,15 +285,6 @@ typedef struct {
} ngx_http_conf_addr_t; } ngx_http_conf_addr_t;
struct ngx_http_server_name_s {
#if (NGX_PCRE)
ngx_http_regex_t *regex;
#endif
ngx_http_core_srv_conf_t *server; /* virtual name server conf */
ngx_str_t name;
};
typedef struct { typedef struct {
ngx_int_t status; ngx_int_t status;
ngx_int_t overwrite; ngx_int_t overwrite;

View File

@ -195,8 +195,99 @@ ngx_http_header_t ngx_http_headers_in[] = {
void void
ngx_http_init_connection(ngx_connection_t *c) ngx_http_init_connection(ngx_connection_t *c)
{ {
ngx_event_t *rev; ngx_uint_t i;
ngx_http_log_ctx_t *ctx; ngx_event_t *rev;
struct sockaddr_in *sin;
ngx_http_port_t *port;
ngx_http_in_addr_t *addr;
ngx_http_log_ctx_t *ctx;
ngx_http_connection_t *hc;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
ngx_http_in6_addr_t *addr6;
#endif
hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
if (hc == NULL) {
ngx_http_close_connection(c);
return;
}
c->data = hc;
/* find the server configuration for the address:port */
port = c->listening->servers;
if (port->naddrs > 1) {
/*
* there are several addresses on this port and one of them
* is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
* is required to determine a server address
*/
if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
ngx_http_close_connection(c);
return;
}
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
addr6 = port->addrs;
/* the last address is "*" */
for (i = 0; i < port->naddrs - 1; i++) {
if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
break;
}
}
hc->addr_conf = &addr6[i].conf;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) c->local_sockaddr;
addr = port->addrs;
/* the last address is "*" */
for (i = 0; i < port->naddrs - 1; i++) {
if (addr[i].addr == sin->sin_addr.s_addr) {
break;
}
}
hc->addr_conf = &addr[i].conf;
break;
}
} else {
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
addr6 = port->addrs;
hc->addr_conf = &addr6[0].conf;
break;
#endif
default: /* AF_INET */
addr = port->addrs;
hc->addr_conf = &addr[0].conf;
break;
}
}
ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t)); ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
if (ctx == NULL) { if (ctx == NULL) {
@ -251,22 +342,13 @@ static void
ngx_http_init_request(ngx_event_t *rev) ngx_http_init_request(ngx_event_t *rev)
{ {
ngx_time_t *tp; ngx_time_t *tp;
ngx_uint_t i;
ngx_connection_t *c; ngx_connection_t *c;
ngx_http_request_t *r; ngx_http_request_t *r;
struct sockaddr_in *sin;
ngx_http_port_t *port;
ngx_http_in_addr_t *addr;
ngx_http_log_ctx_t *ctx; ngx_http_log_ctx_t *ctx;
ngx_http_addr_conf_t *addr_conf;
ngx_http_connection_t *hc; ngx_http_connection_t *hc;
ngx_http_core_srv_conf_t *cscf; ngx_http_core_srv_conf_t *cscf;
ngx_http_core_loc_conf_t *clcf; ngx_http_core_loc_conf_t *clcf;
ngx_http_core_main_conf_t *cmcf; ngx_http_core_main_conf_t *cmcf;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
ngx_http_in6_addr_t *addr6;
#endif
#if (NGX_STAT_STUB) #if (NGX_STAT_STUB)
(void) ngx_atomic_fetch_add(ngx_stat_reading, -1); (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
@ -285,14 +367,6 @@ ngx_http_init_request(ngx_event_t *rev)
hc = c->data; hc = c->data;
if (hc == NULL) {
hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
if (hc == NULL) {
ngx_http_close_connection(c);
return;
}
}
r = hc->request; r = hc->request;
if (r) { if (r) {
@ -320,86 +394,10 @@ ngx_http_init_request(ngx_event_t *rev)
c->sent = 0; c->sent = 0;
r->signature = NGX_HTTP_MODULE; r->signature = NGX_HTTP_MODULE;
/* find the server configuration for the address:port */
port = c->listening->servers;
r->connection = c; r->connection = c;
if (port->naddrs > 1) {
/*
* there are several addresses on this port and one of them
* is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
* is required to determine a server address
*/
if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
ngx_http_close_connection(c);
return;
}
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
addr6 = port->addrs;
/* the last address is "*" */
for (i = 0; i < port->naddrs - 1; i++) {
if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
break;
}
}
addr_conf = &addr6[i].conf;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) c->local_sockaddr;
addr = port->addrs;
/* the last address is "*" */
for (i = 0; i < port->naddrs - 1; i++) {
if (addr[i].addr == sin->sin_addr.s_addr) {
break;
}
}
addr_conf = &addr[i].conf;
break;
}
} else {
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
addr6 = port->addrs;
addr_conf = &addr6[0].conf;
break;
#endif
default: /* AF_INET */
addr = port->addrs;
addr_conf = &addr[0].conf;
break;
}
}
r->virtual_names = addr_conf->virtual_names;
/* the default server configuration for the address:port */ /* the default server configuration for the address:port */
cscf = addr_conf->default_server; cscf = hc->addr_conf->default_server;
r->main_conf = cscf->ctx->main_conf; r->main_conf = cscf->ctx->main_conf;
r->srv_conf = cscf->ctx->srv_conf; r->srv_conf = cscf->ctx->srv_conf;
@ -414,13 +412,13 @@ ngx_http_init_request(ngx_event_t *rev)
ngx_http_ssl_srv_conf_t *sscf; ngx_http_ssl_srv_conf_t *sscf;
sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module); sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
if (sscf->enable || addr_conf->ssl) { if (sscf->enable || hc->addr_conf->ssl) {
if (c->ssl == NULL) { if (c->ssl == NULL) {
c->log->action = "SSL handshaking"; c->log->action = "SSL handshaking";
if (addr_conf->ssl && sscf->ssl.ctx == NULL) { if (hc->addr_conf->ssl && sscf->ssl.ctx == NULL) {
ngx_log_error(NGX_LOG_ERR, c->log, 0, ngx_log_error(NGX_LOG_ERR, c->log, 0,
"no \"ssl_certificate\" is defined " "no \"ssl_certificate\" is defined "
"in server listening on SSL port"); "in server listening on SSL port");
@ -1799,12 +1797,15 @@ ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
{ {
ngx_http_core_loc_conf_t *clcf; ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t *cscf; ngx_http_core_srv_conf_t *cscf;
ngx_http_virtual_names_t *virtual_names;
if (r->virtual_names == NULL) { virtual_names = r->http_connection->addr_conf->virtual_names;
if (virtual_names == NULL) {
return NGX_DECLINED; return NGX_DECLINED;
} }
cscf = ngx_hash_find_combined(&r->virtual_names->names, cscf = ngx_hash_find_combined(&virtual_names->names,
ngx_hash_key(host, len), host, len); ngx_hash_key(host, len), host, len);
if (cscf) { if (cscf) {
@ -1813,7 +1814,7 @@ ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
#if (NGX_PCRE) #if (NGX_PCRE)
if (len && r->virtual_names->nregex) { if (len && virtual_names->nregex) {
ngx_int_t n; ngx_int_t n;
ngx_uint_t i; ngx_uint_t i;
ngx_str_t name; ngx_str_t name;
@ -1822,9 +1823,9 @@ ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
name.len = len; name.len = len;
name.data = host; name.data = host;
sn = r->virtual_names->regex; sn = virtual_names->regex;
for (i = 0; i < r->virtual_names->nregex; i++) { for (i = 0; i < virtual_names->nregex; i++) {
n = ngx_http_regex_exec(r, sn[i].regex, &name); n = ngx_http_regex_exec(r, sn[i].regex, &name);

View File

@ -289,7 +289,11 @@ typedef struct {
} ngx_http_request_body_t; } ngx_http_request_body_t;
typedef struct ngx_http_addr_conf_s ngx_http_addr_conf_t;
typedef struct { typedef struct {
ngx_http_addr_conf_t *addr_conf;
ngx_http_request_t *request; ngx_http_request_t *request;
ngx_buf_t **busy; ngx_buf_t **busy;
@ -302,17 +306,6 @@ typedef struct {
} ngx_http_connection_t; } ngx_http_connection_t;
typedef struct ngx_http_server_name_s ngx_http_server_name_t;
typedef struct {
ngx_hash_combined_t names;
ngx_uint_t nregex;
ngx_http_server_name_t *regex;
} ngx_http_virtual_names_t;
typedef void (*ngx_http_cleanup_pt)(void *data); typedef void (*ngx_http_cleanup_pt)(void *data);
typedef struct ngx_http_cleanup_s ngx_http_cleanup_t; typedef struct ngx_http_cleanup_s ngx_http_cleanup_t;
@ -406,8 +399,6 @@ struct ngx_http_request_s {
ngx_http_post_subrequest_t *post_subrequest; ngx_http_post_subrequest_t *post_subrequest;
ngx_http_posted_request_t *posted_requests; ngx_http_posted_request_t *posted_requests;
ngx_http_virtual_names_t *virtual_names;
ngx_int_t phase_handler; ngx_int_t phase_handler;
ngx_http_handler_pt content_handler; ngx_http_handler_pt content_handler;
ngx_uint_t access_code; ngx_uint_t access_code;