mirror of
https://github.com/nginx/nginx.git
synced 2025-06-09 10:52:47 +08:00
split ports, addresses, and server names preparation and optimization
This commit is contained in:
parent
34abdb1683
commit
a883361c47
@ -19,6 +19,12 @@ static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf,
|
|||||||
|
|
||||||
static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf,
|
static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf,
|
||||||
ngx_array_t *servers, ngx_array_t *in_ports);
|
ngx_array_t *servers, ngx_array_t *in_ports);
|
||||||
|
static ngx_int_t ngx_http_add_ports(ngx_conf_t *cf,
|
||||||
|
ngx_http_core_srv_conf_t *cscf, ngx_array_t *in_ports,
|
||||||
|
ngx_http_listen_t *listen);
|
||||||
|
static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf,
|
||||||
|
ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
|
||||||
|
ngx_http_listen_t *listen);
|
||||||
static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
|
static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
|
||||||
ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
|
ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
|
||||||
ngx_http_listen_t *listen);
|
ngx_http_listen_t *listen);
|
||||||
@ -44,6 +50,8 @@ static ngx_http_location_tree_node_t *
|
|||||||
|
|
||||||
static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
|
static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
|
||||||
ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports);
|
ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports);
|
||||||
|
static ngx_int_t ngx_http_server_names(ngx_conf_t *cf,
|
||||||
|
ngx_http_core_main_conf_t *cmcf, ngx_http_conf_in_addr_t *in_addr);
|
||||||
static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two);
|
static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two);
|
||||||
static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
|
static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
|
||||||
const void *two);
|
const void *two);
|
||||||
@ -1101,10 +1109,8 @@ static ngx_int_t
|
|||||||
ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
|
ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
|
||||||
ngx_array_t *in_ports)
|
ngx_array_t *in_ports)
|
||||||
{
|
{
|
||||||
ngx_uint_t s, l, p, a;
|
ngx_uint_t s, i;
|
||||||
ngx_http_listen_t *listen;
|
ngx_http_listen_t *listen;
|
||||||
ngx_http_conf_in_port_t *in_port;
|
|
||||||
ngx_http_conf_in_addr_t *in_addr;
|
|
||||||
ngx_http_core_srv_conf_t **cscfp;
|
ngx_http_core_srv_conf_t **cscfp;
|
||||||
|
|
||||||
if (ngx_array_init(in_ports, cf->temp_pool, 2,
|
if (ngx_array_init(in_ports, cf->temp_pool, 2,
|
||||||
@ -1122,30 +1128,75 @@ ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
|
|||||||
/* "listen" directives */
|
/* "listen" directives */
|
||||||
|
|
||||||
listen = cscfp[s]->listen.elts;
|
listen = cscfp[s]->listen.elts;
|
||||||
for (l = 0; l < cscfp[s]->listen.nelts; l++) {
|
for (i = 0; i < cscfp[s]->listen.nelts; i++) {
|
||||||
|
|
||||||
/* AF_INET only */
|
/* AF_INET only */
|
||||||
|
|
||||||
in_port = in_ports->elts;
|
if (ngx_http_add_ports(cf, cscfp[s], in_ports, &listen[i])
|
||||||
for (p = 0; p < in_ports->nelts; p++) {
|
!= NGX_OK)
|
||||||
|
{
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (listen[l].port != in_port[p].port) {
|
return NGX_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* AF_INET only */
|
||||||
|
|
||||||
|
static ngx_int_t
|
||||||
|
ngx_http_add_ports(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
|
||||||
|
ngx_array_t *in_ports, ngx_http_listen_t *listen)
|
||||||
|
{
|
||||||
|
ngx_uint_t i;
|
||||||
|
ngx_http_conf_in_port_t *in_port;
|
||||||
|
|
||||||
|
in_port = in_ports->elts;
|
||||||
|
for (i = 0; i < in_ports->nelts; i++) {
|
||||||
|
|
||||||
|
if (listen->port != in_port[i].port) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* the port is already in the port list */
|
/* a port is already in the in_port list */
|
||||||
|
|
||||||
in_addr = in_port[p].addrs.elts;
|
return ngx_http_add_addresses(cf, cscf, &in_port[i], listen);
|
||||||
for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
}
|
||||||
|
|
||||||
if (listen[l].addr != in_addr[a].addr) {
|
/* add a port to the in_port list */
|
||||||
|
|
||||||
|
in_port = ngx_array_push(in_ports);
|
||||||
|
if (in_port == NULL) {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
in_port->port = listen->port;
|
||||||
|
in_port->addrs.elts = NULL;
|
||||||
|
|
||||||
|
return ngx_http_add_address(cf, cscf, in_port, listen);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static ngx_int_t
|
||||||
|
ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
|
||||||
|
ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen)
|
||||||
|
{
|
||||||
|
ngx_uint_t i;
|
||||||
|
ngx_http_conf_in_addr_t *in_addr;
|
||||||
|
|
||||||
|
in_addr = in_port->addrs.elts;
|
||||||
|
|
||||||
|
for (i = 0; i < in_port->addrs.nelts; i++) {
|
||||||
|
|
||||||
|
if (listen->addr != in_addr[i].addr) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* the address is already in the address list */
|
/* the address is already in the address list */
|
||||||
|
|
||||||
if (ngx_http_add_names(cf, cscfp[s], &in_addr[a]) != NGX_OK)
|
if (ngx_http_add_names(cf, cscf, &in_addr[i]) != NGX_OK) {
|
||||||
{
|
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1154,25 +1205,25 @@ ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
|
|||||||
* for this address:port
|
* for this address:port
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (listen[l].conf.default_server) {
|
if (listen->conf.default_server) {
|
||||||
|
|
||||||
if (in_addr[a].default_server) {
|
if (in_addr[i].default_server) {
|
||||||
ngx_log_error(NGX_LOG_ERR, cf->log, 0,
|
ngx_log_error(NGX_LOG_ERR, cf->log, 0,
|
||||||
"the duplicate default server in %s:%ui",
|
"the duplicate default server in %s:%ui",
|
||||||
listen[l].file_name, listen[l].line);
|
listen->file_name, listen->line);
|
||||||
|
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
in_addr[a].core_srv_conf = cscfp[s];
|
in_addr[i].core_srv_conf = cscf;
|
||||||
in_addr[a].default_server = 1;
|
in_addr[i].default_server = 1;
|
||||||
#if (NGX_HTTP_SSL)
|
#if (NGX_HTTP_SSL)
|
||||||
in_addr[a].ssl = listen[l].conf.ssl;
|
in_addr[i].ssl = listen->conf.ssl;
|
||||||
#endif
|
#endif
|
||||||
in_addr[a].listen_conf = &listen[l].conf;
|
in_addr[i].listen_conf = &listen->conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
goto found;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1180,38 +1231,7 @@ ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
|
|||||||
* bound to this port
|
* bound to this port
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (ngx_http_add_address(cf, cscfp[s], &in_port[p], &listen[l])
|
return ngx_http_add_address(cf, cscf, in_port, listen);
|
||||||
!= NGX_OK)
|
|
||||||
{
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
goto found;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* add the port to the in_port list */
|
|
||||||
|
|
||||||
in_port = ngx_array_push(in_ports);
|
|
||||||
if (in_port == NULL) {
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
in_port->port = listen[l].port;
|
|
||||||
in_port->addrs.elts = NULL;
|
|
||||||
|
|
||||||
if (ngx_http_add_address(cf, cscfp[s], in_port, &listen[l])
|
|
||||||
!= NGX_OK)
|
|
||||||
{
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
found:
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NGX_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1309,16 +1329,10 @@ static ngx_int_t
|
|||||||
ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
||||||
ngx_array_t *in_ports)
|
ngx_array_t *in_ports)
|
||||||
{
|
{
|
||||||
ngx_int_t rc;
|
|
||||||
ngx_uint_t s, p, a;
|
ngx_uint_t s, p, a;
|
||||||
ngx_hash_init_t hash;
|
|
||||||
ngx_http_server_name_t *name;
|
ngx_http_server_name_t *name;
|
||||||
ngx_hash_keys_arrays_t ha;
|
|
||||||
ngx_http_conf_in_port_t *in_port;
|
ngx_http_conf_in_port_t *in_port;
|
||||||
ngx_http_conf_in_addr_t *in_addr;
|
ngx_http_conf_in_addr_t *in_addr;
|
||||||
#if (NGX_PCRE)
|
|
||||||
ngx_uint_t regex, i;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
in_port = in_ports->elts;
|
in_port = in_ports->elts;
|
||||||
for (p = 0; p < in_ports->nelts; p++) {
|
for (p = 0; p < in_ports->nelts; p++) {
|
||||||
@ -1327,8 +1341,8 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|||||||
sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
|
sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* check whether all name-based servers have
|
* check whether all name-based servers have the same
|
||||||
* the same configuraiton as the default server
|
* configuraiton as a default server for given address:port
|
||||||
*/
|
*/
|
||||||
|
|
||||||
in_addr = in_port[p].addrs.elts;
|
in_addr = in_port[p].addrs.elts;
|
||||||
@ -1337,22 +1351,41 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|||||||
name = in_addr[a].names.elts;
|
name = in_addr[a].names.elts;
|
||||||
for (s = 0; s < in_addr[a].names.nelts; s++) {
|
for (s = 0; s < in_addr[a].names.nelts; s++) {
|
||||||
|
|
||||||
if (in_addr[a].core_srv_conf != name[s].core_srv_conf) {
|
if (in_addr[a].core_srv_conf == name[s].core_srv_conf) {
|
||||||
goto virtual_names;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* if all name-based servers have the same configuration
|
|
||||||
* as the default server, then we do not need to check
|
|
||||||
* them at run-time at all
|
|
||||||
*/
|
|
||||||
|
|
||||||
in_addr[a].names.nelts = 0;
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
virtual_names:
|
if (ngx_http_server_names(cf, cmcf, &in_addr[a]) != NGX_OK) {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NGX_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static ngx_int_t
|
||||||
|
ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
||||||
|
ngx_http_conf_in_addr_t *in_addr)
|
||||||
|
{
|
||||||
|
ngx_int_t rc;
|
||||||
|
ngx_uint_t s;
|
||||||
|
ngx_hash_init_t hash;
|
||||||
|
ngx_http_server_name_t *name;
|
||||||
|
ngx_hash_keys_arrays_t ha;
|
||||||
|
#if (NGX_PCRE)
|
||||||
|
ngx_uint_t regex, i;
|
||||||
|
|
||||||
|
regex = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
|
ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
|
||||||
|
|
||||||
@ -1367,13 +1400,9 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if (NGX_PCRE)
|
name = in_addr->names.elts;
|
||||||
regex = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
name = in_addr[a].names.elts;
|
for (s = 0; s < in_addr->names.nelts; s++) {
|
||||||
|
|
||||||
for (s = 0; s < in_addr[a].names.nelts; s++) {
|
|
||||||
|
|
||||||
#if (NGX_PCRE)
|
#if (NGX_PCRE)
|
||||||
if (name[s].regex) {
|
if (name[s].regex) {
|
||||||
@ -1392,14 +1421,14 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|||||||
if (rc == NGX_DECLINED) {
|
if (rc == NGX_DECLINED) {
|
||||||
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
||||||
"invalid server name or wildcard \"%V\" on %s",
|
"invalid server name or wildcard \"%V\" on %s",
|
||||||
&name[s].name, in_addr[a].listen_conf->addr);
|
&name[s].name, in_addr->listen_conf->addr);
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc == NGX_BUSY) {
|
if (rc == NGX_BUSY) {
|
||||||
ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
||||||
"conflicting server name \"%V\" on %s, ignored",
|
"conflicting server name \"%V\" on %s, ignored",
|
||||||
&name[s].name, in_addr[a].listen_conf->addr);
|
&name[s].name, in_addr->listen_conf->addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1410,21 +1439,18 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|||||||
hash.pool = cf->pool;
|
hash.pool = cf->pool;
|
||||||
|
|
||||||
if (ha.keys.nelts) {
|
if (ha.keys.nelts) {
|
||||||
hash.hash = &in_addr[a].hash;
|
hash.hash = &in_addr->hash;
|
||||||
hash.temp_pool = NULL;
|
hash.temp_pool = NULL;
|
||||||
|
|
||||||
if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK)
|
if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) {
|
||||||
{
|
|
||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ha.dns_wc_head.nelts) {
|
if (ha.dns_wc_head.nelts) {
|
||||||
|
|
||||||
ngx_qsort(ha.dns_wc_head.elts,
|
ngx_qsort(ha.dns_wc_head.elts, (size_t) ha.dns_wc_head.nelts,
|
||||||
(size_t) ha.dns_wc_head.nelts,
|
sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);
|
||||||
sizeof(ngx_hash_key_t),
|
|
||||||
ngx_http_cmp_dns_wildcards);
|
|
||||||
|
|
||||||
hash.hash = NULL;
|
hash.hash = NULL;
|
||||||
hash.temp_pool = ha.temp_pool;
|
hash.temp_pool = ha.temp_pool;
|
||||||
@ -1436,15 +1462,13 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
in_addr[a].wc_head = (ngx_hash_wildcard_t *) hash.hash;
|
in_addr->wc_head = (ngx_hash_wildcard_t *) hash.hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ha.dns_wc_tail.nelts) {
|
if (ha.dns_wc_tail.nelts) {
|
||||||
|
|
||||||
ngx_qsort(ha.dns_wc_tail.elts,
|
ngx_qsort(ha.dns_wc_tail.elts, (size_t) ha.dns_wc_tail.nelts,
|
||||||
(size_t) ha.dns_wc_tail.nelts,
|
sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);
|
||||||
sizeof(ngx_hash_key_t),
|
|
||||||
ngx_http_cmp_dns_wildcards);
|
|
||||||
|
|
||||||
hash.hash = NULL;
|
hash.hash = NULL;
|
||||||
hash.temp_pool = ha.temp_pool;
|
hash.temp_pool = ha.temp_pool;
|
||||||
@ -1456,7 +1480,7 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
in_addr[a].wc_tail = (ngx_hash_wildcard_t *) hash.hash;
|
in_addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx_destroy_pool(ha.temp_pool);
|
ngx_destroy_pool(ha.temp_pool);
|
||||||
@ -1464,29 +1488,23 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|||||||
#if (NGX_PCRE)
|
#if (NGX_PCRE)
|
||||||
|
|
||||||
if (regex == 0) {
|
if (regex == 0) {
|
||||||
continue;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
in_addr[a].nregex = regex;
|
in_addr->nregex = regex;
|
||||||
in_addr[a].regex = ngx_palloc(cf->pool,
|
in_addr->regex = ngx_palloc(cf->pool,
|
||||||
regex * sizeof(ngx_http_server_name_t));
|
regex * sizeof(ngx_http_server_name_t));
|
||||||
|
if (in_addr->regex == NULL) {
|
||||||
if (in_addr[a].regex == NULL) {
|
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0, s = 0; s < in_addr[a].names.nelts; s++) {
|
for (i = 0, s = 0; s < in_addr->names.nelts; s++) {
|
||||||
if (name[s].regex) {
|
if (name[s].regex) {
|
||||||
in_addr[a].regex[i++] = name[s];
|
in_addr->regex[i++] = name[s];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) {
|
#endif
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user