comparison src/http/ngx_http.c @ 2511:f91c5c964e61

split ports, addresses, and server names preparation and optimization
author Igor Sysoev <igor@sysoev.ru>
date Wed, 18 Feb 2009 16:17:12 +0000
parents 16d1236bc298
children 2e91aecb9e57
comparison
equal deleted inserted replaced
2510:fa03c3846ca4 2511:f91c5c964e61
17 static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf, 17 static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf,
18 ngx_http_core_main_conf_t *cmcf); 18 ngx_http_core_main_conf_t *cmcf);
19 19
20 static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf, 20 static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf,
21 ngx_array_t *servers, ngx_array_t *in_ports); 21 ngx_array_t *servers, ngx_array_t *in_ports);
22 static ngx_int_t ngx_http_add_ports(ngx_conf_t *cf,
23 ngx_http_core_srv_conf_t *cscf, ngx_array_t *in_ports,
24 ngx_http_listen_t *listen);
25 static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf,
26 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
27 ngx_http_listen_t *listen);
22 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf, 28 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
23 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port, 29 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
24 ngx_http_listen_t *listen); 30 ngx_http_listen_t *listen);
25 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf, 31 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
26 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_addr_t *in_addr); 32 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_addr_t *in_addr);
42 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations, 48 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
43 size_t prefix); 49 size_t prefix);
44 50
45 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf, 51 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
46 ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports); 52 ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports);
53 static ngx_int_t ngx_http_server_names(ngx_conf_t *cf,
54 ngx_http_core_main_conf_t *cmcf, ngx_http_conf_in_addr_t *in_addr);
47 static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two); 55 static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two);
48 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one, 56 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
49 const void *two); 57 const void *two);
50 58
51 static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf, 59 static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf,
1099 1107
1100 static ngx_int_t 1108 static ngx_int_t
1101 ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers, 1109 ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
1102 ngx_array_t *in_ports) 1110 ngx_array_t *in_ports)
1103 { 1111 {
1104 ngx_uint_t s, l, p, a; 1112 ngx_uint_t s, i;
1105 ngx_http_listen_t *listen; 1113 ngx_http_listen_t *listen;
1106 ngx_http_conf_in_port_t *in_port;
1107 ngx_http_conf_in_addr_t *in_addr;
1108 ngx_http_core_srv_conf_t **cscfp; 1114 ngx_http_core_srv_conf_t **cscfp;
1109 1115
1110 if (ngx_array_init(in_ports, cf->temp_pool, 2, 1116 if (ngx_array_init(in_ports, cf->temp_pool, 2,
1111 sizeof(ngx_http_conf_in_port_t)) 1117 sizeof(ngx_http_conf_in_port_t))
1112 != NGX_OK) 1118 != NGX_OK)
1120 for (s = 0; s < servers->nelts; s++) { 1126 for (s = 0; s < servers->nelts; s++) {
1121 1127
1122 /* "listen" directives */ 1128 /* "listen" directives */
1123 1129
1124 listen = cscfp[s]->listen.elts; 1130 listen = cscfp[s]->listen.elts;
1125 for (l = 0; l < cscfp[s]->listen.nelts; l++) { 1131 for (i = 0; i < cscfp[s]->listen.nelts; i++) {
1126 1132
1127 /* AF_INET only */ 1133 /* AF_INET only */
1128 1134
1129 in_port = in_ports->elts; 1135 if (ngx_http_add_ports(cf, cscfp[s], in_ports, &listen[i])
1130 for (p = 0; p < in_ports->nelts; p++) {
1131
1132 if (listen[l].port != in_port[p].port) {
1133 continue;
1134 }
1135
1136 /* the port is already in the port list */
1137
1138 in_addr = in_port[p].addrs.elts;
1139 for (a = 0; a < in_port[p].addrs.nelts; a++) {
1140
1141 if (listen[l].addr != in_addr[a].addr) {
1142 continue;
1143 }
1144
1145 /* the address is already in the address list */
1146
1147 if (ngx_http_add_names(cf, cscfp[s], &in_addr[a]) != NGX_OK)
1148 {
1149 return NGX_ERROR;
1150 }
1151
1152 /*
1153 * check the duplicate "default" server
1154 * for this address:port
1155 */
1156
1157 if (listen[l].conf.default_server) {
1158
1159 if (in_addr[a].default_server) {
1160 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
1161 "the duplicate default server in %s:%ui",
1162 listen[l].file_name, listen[l].line);
1163
1164 return NGX_ERROR;
1165 }
1166
1167 in_addr[a].core_srv_conf = cscfp[s];
1168 in_addr[a].default_server = 1;
1169 #if (NGX_HTTP_SSL)
1170 in_addr[a].ssl = listen[l].conf.ssl;
1171 #endif
1172 in_addr[a].listen_conf = &listen[l].conf;
1173 }
1174
1175 goto found;
1176 }
1177
1178 /*
1179 * add the address to the addresses list that
1180 * bound to this port
1181 */
1182
1183 if (ngx_http_add_address(cf, cscfp[s], &in_port[p], &listen[l])
1184 != NGX_OK)
1185 {
1186 return NGX_ERROR;
1187 }
1188
1189 goto found;
1190 }
1191
1192 /* add the port to the in_port list */
1193
1194 in_port = ngx_array_push(in_ports);
1195 if (in_port == NULL) {
1196 return NGX_ERROR;
1197 }
1198
1199 in_port->port = listen[l].port;
1200 in_port->addrs.elts = NULL;
1201
1202 if (ngx_http_add_address(cf, cscfp[s], in_port, &listen[l])
1203 != NGX_OK) 1136 != NGX_OK)
1204 { 1137 {
1205 return NGX_ERROR; 1138 return NGX_ERROR;
1206 } 1139 }
1207 1140 }
1208 found: 1141 }
1209 1142
1143 return NGX_OK;
1144 }
1145
1146
1147 /* AF_INET only */
1148
1149 static ngx_int_t
1150 ngx_http_add_ports(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1151 ngx_array_t *in_ports, ngx_http_listen_t *listen)
1152 {
1153 ngx_uint_t i;
1154 ngx_http_conf_in_port_t *in_port;
1155
1156 in_port = in_ports->elts;
1157 for (i = 0; i < in_ports->nelts; i++) {
1158
1159 if (listen->port != in_port[i].port) {
1210 continue; 1160 continue;
1211 } 1161 }
1212 } 1162
1213 1163 /* a port is already in the in_port list */
1214 return NGX_OK; 1164
1165 return ngx_http_add_addresses(cf, cscf, &in_port[i], listen);
1166 }
1167
1168 /* add a port to the in_port list */
1169
1170 in_port = ngx_array_push(in_ports);
1171 if (in_port == NULL) {
1172 return NGX_ERROR;
1173 }
1174
1175 in_port->port = listen->port;
1176 in_port->addrs.elts = NULL;
1177
1178 return ngx_http_add_address(cf, cscf, in_port, listen);
1179 }
1180
1181
1182 static ngx_int_t
1183 ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1184 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen)
1185 {
1186 ngx_uint_t i;
1187 ngx_http_conf_in_addr_t *in_addr;
1188
1189 in_addr = in_port->addrs.elts;
1190
1191 for (i = 0; i < in_port->addrs.nelts; i++) {
1192
1193 if (listen->addr != in_addr[i].addr) {
1194 continue;
1195 }
1196
1197 /* the address is already in the address list */
1198
1199 if (ngx_http_add_names(cf, cscf, &in_addr[i]) != NGX_OK) {
1200 return NGX_ERROR;
1201 }
1202
1203 /*
1204 * check the duplicate "default" server
1205 * for this address:port
1206 */
1207
1208 if (listen->conf.default_server) {
1209
1210 if (in_addr[i].default_server) {
1211 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
1212 "the duplicate default server in %s:%ui",
1213 listen->file_name, listen->line);
1214
1215 return NGX_ERROR;
1216 }
1217
1218 in_addr[i].core_srv_conf = cscf;
1219 in_addr[i].default_server = 1;
1220 #if (NGX_HTTP_SSL)
1221 in_addr[i].ssl = listen->conf.ssl;
1222 #endif
1223 in_addr[i].listen_conf = &listen->conf;
1224 }
1225
1226 return NGX_OK;
1227 }
1228
1229 /*
1230 * add the address to the addresses list that
1231 * bound to this port
1232 */
1233
1234 return ngx_http_add_address(cf, cscf, in_port, listen);
1215 } 1235 }
1216 1236
1217 1237
1218 /* 1238 /*
1219 * add the server address, the server names and the server core module 1239 * add the server address, the server names and the server core module
1220 * configurations to the port (in_port) 1240 * configurations to the port (in_port)
1221 */ 1241 */
1222 1242
1223 static ngx_int_t 1243 static ngx_int_t
1224 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, 1244 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1225 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen) 1245 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen)
1226 { 1246 {
1227 ngx_http_conf_in_addr_t *in_addr; 1247 ngx_http_conf_in_addr_t *in_addr;
1228 1248
1229 if (in_port->addrs.elts == NULL) { 1249 if (in_port->addrs.elts == NULL) {
1230 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4, 1250 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4,
1307 1327
1308 static ngx_int_t 1328 static ngx_int_t
1309 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, 1329 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1310 ngx_array_t *in_ports) 1330 ngx_array_t *in_ports)
1311 { 1331 {
1332 ngx_uint_t s, p, a;
1333 ngx_http_server_name_t *name;
1334 ngx_http_conf_in_port_t *in_port;
1335 ngx_http_conf_in_addr_t *in_addr;
1336
1337 in_port = in_ports->elts;
1338 for (p = 0; p < in_ports->nelts; p++) {
1339
1340 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
1341 sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
1342
1343 /*
1344 * check whether all name-based servers have the same
1345 * configuraiton as a default server for given address:port
1346 */
1347
1348 in_addr = in_port[p].addrs.elts;
1349 for (a = 0; a < in_port[p].addrs.nelts; a++) {
1350
1351 name = in_addr[a].names.elts;
1352 for (s = 0; s < in_addr[a].names.nelts; s++) {
1353
1354 if (in_addr[a].core_srv_conf == name[s].core_srv_conf) {
1355 continue;
1356 }
1357
1358 if (ngx_http_server_names(cf, cmcf, &in_addr[a]) != NGX_OK) {
1359 return NGX_ERROR;
1360 }
1361
1362 break;
1363 }
1364 }
1365
1366 if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) {
1367 return NGX_ERROR;
1368 }
1369 }
1370
1371 return NGX_OK;
1372 }
1373
1374
1375 static ngx_int_t
1376 ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1377 ngx_http_conf_in_addr_t *in_addr)
1378 {
1312 ngx_int_t rc; 1379 ngx_int_t rc;
1313 ngx_uint_t s, p, a; 1380 ngx_uint_t s;
1314 ngx_hash_init_t hash; 1381 ngx_hash_init_t hash;
1315 ngx_http_server_name_t *name; 1382 ngx_http_server_name_t *name;
1316 ngx_hash_keys_arrays_t ha; 1383 ngx_hash_keys_arrays_t ha;
1317 ngx_http_conf_in_port_t *in_port;
1318 ngx_http_conf_in_addr_t *in_addr;
1319 #if (NGX_PCRE) 1384 #if (NGX_PCRE)
1320 ngx_uint_t regex, i; 1385 ngx_uint_t regex, i;
1386
1387 regex = 0;
1321 #endif 1388 #endif
1322 1389
1323 in_port = in_ports->elts; 1390 ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
1324 for (p = 0; p < in_ports->nelts; p++) { 1391
1325 1392 ha.temp_pool = ngx_create_pool(16384, cf->log);
1326 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts, 1393 if (ha.temp_pool == NULL) {
1327 sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs); 1394 return NGX_ERROR;
1328 1395 }
1329 /* 1396
1330 * check whether all name-based servers have 1397 ha.pool = cf->pool;
1331 * the same configuraiton as the default server 1398
1332 */ 1399 if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
1333 1400 goto failed;
1334 in_addr = in_port[p].addrs.elts; 1401 }
1335 for (a = 0; a < in_port[p].addrs.nelts; a++) { 1402
1336 1403 name = in_addr->names.elts;
1337 name = in_addr[a].names.elts; 1404
1338 for (s = 0; s < in_addr[a].names.nelts; s++) { 1405 for (s = 0; s < in_addr->names.nelts; s++) {
1339 1406
1340 if (in_addr[a].core_srv_conf != name[s].core_srv_conf) { 1407 #if (NGX_PCRE)
1341 goto virtual_names; 1408 if (name[s].regex) {
1342 } 1409 regex++;
1343 }
1344
1345 /*
1346 * if all name-based servers have the same configuration
1347 * as the default server, then we do not need to check
1348 * them at run-time at all
1349 */
1350
1351 in_addr[a].names.nelts = 0;
1352
1353 continue; 1410 continue;
1354 1411 }
1355 virtual_names: 1412 #endif
1356 1413
1357 ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t)); 1414 rc = ngx_hash_add_key(&ha, &name[s].name, name[s].core_srv_conf,
1358 1415 NGX_HASH_WILDCARD_KEY);
1359 ha.temp_pool = ngx_create_pool(16384, cf->log); 1416
1360 if (ha.temp_pool == NULL) { 1417 if (rc == NGX_ERROR) {
1361 return NGX_ERROR; 1418 return NGX_ERROR;
1362 } 1419 }
1363 1420
1364 ha.pool = cf->pool; 1421 if (rc == NGX_DECLINED) {
1365 1422 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
1366 if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) { 1423 "invalid server name or wildcard \"%V\" on %s",
1367 goto failed; 1424 &name[s].name, in_addr->listen_conf->addr);
1368 } 1425 return NGX_ERROR;
1426 }
1427
1428 if (rc == NGX_BUSY) {
1429 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
1430 "conflicting server name \"%V\" on %s, ignored",
1431 &name[s].name, in_addr->listen_conf->addr);
1432 }
1433 }
1434
1435 hash.key = ngx_hash_key_lc;
1436 hash.max_size = cmcf->server_names_hash_max_size;
1437 hash.bucket_size = cmcf->server_names_hash_bucket_size;
1438 hash.name = "server_names_hash";
1439 hash.pool = cf->pool;
1440
1441 if (ha.keys.nelts) {
1442 hash.hash = &in_addr->hash;
1443 hash.temp_pool = NULL;
1444
1445 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) {
1446 goto failed;
1447 }
1448 }
1449
1450 if (ha.dns_wc_head.nelts) {
1451
1452 ngx_qsort(ha.dns_wc_head.elts, (size_t) ha.dns_wc_head.nelts,
1453 sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);
1454
1455 hash.hash = NULL;
1456 hash.temp_pool = ha.temp_pool;
1457
1458 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
1459 ha.dns_wc_head.nelts)
1460 != NGX_OK)
1461 {
1462 goto failed;
1463 }
1464
1465 in_addr->wc_head = (ngx_hash_wildcard_t *) hash.hash;
1466 }
1467
1468 if (ha.dns_wc_tail.nelts) {
1469
1470 ngx_qsort(ha.dns_wc_tail.elts, (size_t) ha.dns_wc_tail.nelts,
1471 sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards);
1472
1473 hash.hash = NULL;
1474 hash.temp_pool = ha.temp_pool;
1475
1476 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts,
1477 ha.dns_wc_tail.nelts)
1478 != NGX_OK)
1479 {
1480 goto failed;
1481 }
1482
1483 in_addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash;
1484 }
1485
1486 ngx_destroy_pool(ha.temp_pool);
1369 1487
1370 #if (NGX_PCRE) 1488 #if (NGX_PCRE)
1371 regex = 0; 1489
1490 if (regex == 0) {
1491 return NGX_OK;
1492 }
1493
1494 in_addr->nregex = regex;
1495 in_addr->regex = ngx_palloc(cf->pool,
1496 regex * sizeof(ngx_http_server_name_t));
1497 if (in_addr->regex == NULL) {
1498 return NGX_ERROR;
1499 }
1500
1501 for (i = 0, s = 0; s < in_addr->names.nelts; s++) {
1502 if (name[s].regex) {
1503 in_addr->regex[i++] = name[s];
1504 }
1505 }
1506
1372 #endif 1507 #endif
1373
1374 name = in_addr[a].names.elts;
1375
1376 for (s = 0; s < in_addr[a].names.nelts; s++) {
1377
1378 #if (NGX_PCRE)
1379 if (name[s].regex) {
1380 regex++;
1381 continue;
1382 }
1383 #endif
1384
1385 rc = ngx_hash_add_key(&ha, &name[s].name, name[s].core_srv_conf,
1386 NGX_HASH_WILDCARD_KEY);
1387
1388 if (rc == NGX_ERROR) {
1389 return NGX_ERROR;
1390 }
1391
1392 if (rc == NGX_DECLINED) {
1393 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
1394 "invalid server name or wildcard \"%V\" on %s",
1395 &name[s].name, in_addr[a].listen_conf->addr);
1396 return NGX_ERROR;
1397 }
1398
1399 if (rc == NGX_BUSY) {
1400 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
1401 "conflicting server name \"%V\" on %s, ignored",
1402 &name[s].name, in_addr[a].listen_conf->addr);
1403 }
1404 }
1405
1406 hash.key = ngx_hash_key_lc;
1407 hash.max_size = cmcf->server_names_hash_max_size;
1408 hash.bucket_size = cmcf->server_names_hash_bucket_size;
1409 hash.name = "server_names_hash";
1410 hash.pool = cf->pool;
1411
1412 if (ha.keys.nelts) {
1413 hash.hash = &in_addr[a].hash;
1414 hash.temp_pool = NULL;
1415
1416 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK)
1417 {
1418 goto failed;
1419 }
1420 }
1421
1422 if (ha.dns_wc_head.nelts) {
1423
1424 ngx_qsort(ha.dns_wc_head.elts,
1425 (size_t) ha.dns_wc_head.nelts,
1426 sizeof(ngx_hash_key_t),
1427 ngx_http_cmp_dns_wildcards);
1428
1429 hash.hash = NULL;
1430 hash.temp_pool = ha.temp_pool;
1431
1432 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
1433 ha.dns_wc_head.nelts)
1434 != NGX_OK)
1435 {
1436 goto failed;
1437 }
1438
1439 in_addr[a].wc_head = (ngx_hash_wildcard_t *) hash.hash;
1440 }
1441
1442 if (ha.dns_wc_tail.nelts) {
1443
1444 ngx_qsort(ha.dns_wc_tail.elts,
1445 (size_t) ha.dns_wc_tail.nelts,
1446 sizeof(ngx_hash_key_t),
1447 ngx_http_cmp_dns_wildcards);
1448
1449 hash.hash = NULL;
1450 hash.temp_pool = ha.temp_pool;
1451
1452 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts,
1453 ha.dns_wc_tail.nelts)
1454 != NGX_OK)
1455 {
1456 goto failed;
1457 }
1458
1459 in_addr[a].wc_tail = (ngx_hash_wildcard_t *) hash.hash;
1460 }
1461
1462 ngx_destroy_pool(ha.temp_pool);
1463
1464 #if (NGX_PCRE)
1465
1466 if (regex == 0) {
1467 continue;
1468 }
1469
1470 in_addr[a].nregex = regex;
1471 in_addr[a].regex = ngx_palloc(cf->pool,
1472 regex * sizeof(ngx_http_server_name_t));
1473
1474 if (in_addr[a].regex == NULL) {
1475 return NGX_ERROR;
1476 }
1477
1478 for (i = 0, s = 0; s < in_addr[a].names.nelts; s++) {
1479 if (name[s].regex) {
1480 in_addr[a].regex[i++] = name[s];
1481 }
1482 }
1483 #endif
1484 }
1485
1486 if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) {
1487 return NGX_ERROR;
1488 }
1489 }
1490 1508
1491 return NGX_OK; 1509 return NGX_OK;
1492 1510
1493 failed: 1511 failed:
1494 1512