nginx http 监听配置解析及虚拟主机的实现

2015-09-14 17:59:10   最后更新: 2015-09-20 22:50:01   访问数量:666




// static char * // ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) // HTTP listen 配置解析 {{{ static char * ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_core_srv_conf_t *cscf = conf; ngx_str_t *value, size; ngx_url_t u; ngx_uint_t n; ngx_http_listen_opt_t lsopt; cscf->listen = 1; value = cf->args->elts; ngx_memzero(&u, sizeof(ngx_url_t)); // value ["listen", "192.168.1.2:9200"] u.url = value[1]; u.listen = 1; u.default_port = 80; // 解析 url 到 ngx_url_t 结构 if (ngx_parse_url(cf->pool, &u) != NGX_OK) { if (u.err) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s in \"%V\" of the \"listen\" directive", u.err, &u.url); } return NGX_CONF_ERROR; } // 初始化 socket 配置结构 ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t)); ngx_memcpy(&lsopt.u.sockaddr, u.sockaddr, u.socklen); lsopt.socklen = u.socklen; lsopt.backlog = NGX_LISTEN_BACKLOG; lsopt.rcvbuf = -1; lsopt.sndbuf = -1; #if (NGX_HAVE_SETFIB) lsopt.setfib = -1; #endif #if (NGX_HAVE_TCP_FASTOPEN) lsopt.fastopen = -1; #endif lsopt.wildcard = u.wildcard; #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) lsopt.ipv6only = 1; #endif (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr, NGX_SOCKADDR_STRLEN, 1); for (n = 2; n < cf->args->nelts; n++) { if (ngx_strcmp(value[n].data, "default_server") == 0 || ngx_strcmp(value[n].data, "default") == 0) { lsopt.default_server = 1; continue; } if (ngx_strcmp(value[n].data, "bind") == 0) { lsopt.set = 1; lsopt.bind = 1; continue; } #if (NGX_HAVE_SETFIB) if (ngx_strncmp(value[n].data, "setfib=", 7) == 0) { lsopt.setfib = ngx_atoi(value[n].data + 7, value[n].len - 7); lsopt.set = 1; lsopt.bind = 1; if (lsopt.setfib == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid setfib \"%V\"", &value[n]); return NGX_CONF_ERROR; } continue; } #endif #if (NGX_HAVE_TCP_FASTOPEN) if (ngx_strncmp(value[n].data, "fastopen=", 9) == 0) { lsopt.fastopen = ngx_atoi(value[n].data + 9, value[n].len - 9); lsopt.set = 1; lsopt.bind = 1; if (lsopt.fastopen == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid fastopen \"%V\"", &value[n]); return NGX_CONF_ERROR; } continue; } #endif if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) { lsopt.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8); lsopt.set = 1; lsopt.bind = 1; if (lsopt.backlog == NGX_ERROR || lsopt.backlog == 0) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid backlog \"%V\"", &value[n]); return NGX_CONF_ERROR; } continue; } if (ngx_strncmp(value[n].data, "rcvbuf=", 7) == 0) { size.len = value[n].len - 7; size.data = value[n].data + 7; lsopt.rcvbuf = ngx_parse_size(&size); lsopt.set = 1; lsopt.bind = 1; if (lsopt.rcvbuf == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid rcvbuf \"%V\"", &value[n]); return NGX_CONF_ERROR; } continue; } if (ngx_strncmp(value[n].data, "sndbuf=", 7) == 0) { size.len = value[n].len - 7; size.data = value[n].data + 7; lsopt.sndbuf = ngx_parse_size(&size); lsopt.set = 1; lsopt.bind = 1; if (lsopt.sndbuf == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid sndbuf \"%V\"", &value[n]); return NGX_CONF_ERROR; } continue; } if (ngx_strncmp(value[n].data, "accept_filter=", 14) == 0) { #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) lsopt.accept_filter = (char *) &value[n].data[14]; lsopt.set = 1; lsopt.bind = 1; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "accept filters \"%V\" are not supported " "on this platform, ignored", &value[n]); #endif continue; } if (ngx_strcmp(value[n].data, "deferred") == 0) { #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) lsopt.deferred_accept = 1; lsopt.set = 1; lsopt.bind = 1; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the deferred accept is not supported " "on this platform, ignored"); #endif continue; } if (ngx_strncmp(value[n].data, "ipv6only=o", 10) == 0) { #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) struct sockaddr *sa; sa = &lsopt.u.sockaddr; if (sa->sa_family == AF_INET6) { if (ngx_strcmp(&value[n].data[10], "n") == 0) { lsopt.ipv6only = 1; } else if (ngx_strcmp(&value[n].data[10], "ff") == 0) { lsopt.ipv6only = 0; } else { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid ipv6only flags \"%s\"", &value[n].data[9]); return NGX_CONF_ERROR; } lsopt.set = 1; lsopt.bind = 1; } else { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "ipv6only is not supported " "on addr \"%s\", ignored", lsopt.addr); } continue; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "ipv6only is not supported " "on this platform"); return NGX_CONF_ERROR; #endif } if (ngx_strcmp(value[n].data, "ssl") == 0) { #if (NGX_HTTP_SSL) lsopt.ssl = 1; continue; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the \"ssl\" parameter requires " "ngx_http_ssl_module"); return NGX_CONF_ERROR; #endif } if (ngx_strcmp(value[n].data, "spdy") == 0) { #if (NGX_HTTP_SPDY) lsopt.spdy = 1; continue; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the \"spdy\" parameter requires " "ngx_http_spdy_module"); return NGX_CONF_ERROR; #endif } if (ngx_strncmp(value[n].data, "so_keepalive=", 13) == 0) { if (ngx_strcmp(&value[n].data[13], "on") == 0) { lsopt.so_keepalive = 1; } else if (ngx_strcmp(&value[n].data[13], "off") == 0) { lsopt.so_keepalive = 2; } else { #if (NGX_HAVE_KEEPALIVE_TUNABLE) u_char *p, *end; ngx_str_t s; end = value[n].data + value[n].len; s.data = value[n].data + 13; p = ngx_strlchr(s.data, end, ':'); if (p == NULL) { p = end; } if (p > s.data) { s.len = p - s.data; lsopt.tcp_keepidle = ngx_parse_time(&s, 1); if (lsopt.tcp_keepidle == (time_t) NGX_ERROR) { goto invalid_so_keepalive; } } s.data = (p < end) ? (p + 1) : end; p = ngx_strlchr(s.data, end, ':'); if (p == NULL) { p = end; } if (p > s.data) { s.len = p - s.data; lsopt.tcp_keepintvl = ngx_parse_time(&s, 1); if (lsopt.tcp_keepintvl == (time_t) NGX_ERROR) { goto invalid_so_keepalive; } } s.data = (p < end) ? (p + 1) : end; if (s.data < end) { s.len = end - s.data; lsopt.tcp_keepcnt = ngx_atoi(s.data, s.len); if (lsopt.tcp_keepcnt == NGX_ERROR) { goto invalid_so_keepalive; } } if (lsopt.tcp_keepidle == 0 && lsopt.tcp_keepintvl == 0 && lsopt.tcp_keepcnt == 0) { goto invalid_so_keepalive; } lsopt.so_keepalive = 1; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the \"so_keepalive\" parameter accepts " "only \"on\" or \"off\" on this platform"); return NGX_CONF_ERROR; #endif } lsopt.set = 1; lsopt.bind = 1; continue; #if (NGX_HAVE_KEEPALIVE_TUNABLE) invalid_so_keepalive: ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid so_keepalive value: \"%s\"", &value[n].data[13]); return NGX_CONF_ERROR; #endif } if (ngx_strcmp(value[n].data, "proxy_protocol") == 0) { lsopt.proxy_protocol = 1; continue; } ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", &value[n]); return NGX_CONF_ERROR; } // 将监听连接加入 if (ngx_http_add_listen(cf, cscf, &lsopt) == NGX_OK) { return NGX_CONF_OK; } return NGX_CONF_ERROR; } // }}}

 

这个函数主要的工作是将配置文件中 http 模块的 server 中的 listen 配置解析出来,以便在 server 配置解析完成后对指定IP、端口进行监听

虚拟主机的解析也是在这个函数中完成的

 

他的主要工作是配置文件 listen 命令的解析,并调用 ngx_http_add_listen 将解析出的 ip 和 port 加入到配置结构的 ports 数组中

 

// ngx_int_t // ngx_http_add_listen(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, // ngx_http_listen_opt_t *lsopt) // 将 ip、port 加入配置结构的 ports 数组中 {{{ ngx_int_t ngx_http_add_listen(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, ngx_http_listen_opt_t *lsopt) { in_port_t p; ngx_uint_t i; struct sockaddr *sa; struct sockaddr_in *sin; ngx_http_conf_port_t *port; ngx_http_core_main_conf_t *cmcf; #if (NGX_HAVE_INET6) struct sockaddr_in6 *sin6; #endif cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); if (cmcf->ports == NULL) { cmcf->ports = ngx_array_create(cf->temp_pool, 2, sizeof(ngx_http_conf_port_t)); if (cmcf->ports == NULL) { return NGX_ERROR; } } sa = &lsopt->u.sockaddr; switch (sa->sa_family) { #if (NGX_HAVE_INET6) case AF_INET6: sin6 = &lsopt->u.sockaddr_in6; p = sin6->sin6_port; break; #endif #if (NGX_HAVE_UNIX_DOMAIN) case AF_UNIX: p = 0; break; #endif default: /* AF_INET */ sin = &lsopt->u.sockaddr_in; p = sin->sin_port; break; } port = cmcf->ports->elts; for (i = 0; i < cmcf->ports->nelts; i++) { if (p != port[i].port || sa->sa_family != port[i].family) { continue; } /* a port is already in the port list */ // 将当前 IP 添加到对应的 port 下的 address 数组中 return ngx_http_add_addresses(cf, cscf, &port[i], lsopt); } /* add a port to the port list */ // 将尚未添加到 port 数组中的 port 加入 ports 数组中 port = ngx_array_push(cmcf->ports); if (port == NULL) { return NGX_ERROR; } port->family = sa->sa_family; port->port = p; port->addrs.elts = NULL; // 创建新的 port 结构,并将 ip 添加到对应的 address 数组中 return ngx_http_add_address(cf, cscf, port, lsopt); } // }}}

 

 

在 ngx_http_core_listen 调用了该函数,用来将解析到的 ip 和 port 加入到 配置结构的 ports 数组中

这个函数的整体逻辑比较简单,主要分为下面两种情况:

  1. 对于已经存在的 port,即每个 port 对应多个 ip 的情况,或相同 IP、port 对应多个虚拟主机的情况,调用 ngx_http_add_addresses 将当前 IP 添加到对应的 port 下的 address 数组中
  2. 而对于首次添加的 port,则调用 ngx_http_add_address 创建 port 结构并存入对应的 IP 与 port

 

虚拟主机配置解析 -- ngx_http_add_addresses

// static ngx_int_t // ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, // ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt) // 解析虚拟主机配置,然后将地址加入 ports数组中 {{{ static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt) { u_char *p; size_t len, off; ngx_uint_t i, default_server; struct sockaddr *sa; ngx_http_conf_addr_t *addr; #if (NGX_HAVE_UNIX_DOMAIN) struct sockaddr_un *saun; #endif #if (NGX_HTTP_SSL) ngx_uint_t ssl; #endif #if (NGX_HTTP_SPDY) ngx_uint_t spdy; #endif /* * we cannot compare whole sockaddr struct's as kernel * may fill some fields in inherited sockaddr struct's */ sa = &lsopt->u.sockaddr; switch (sa->sa_family) { #if (NGX_HAVE_INET6) case AF_INET6: off = offsetof(struct sockaddr_in6, sin6_addr); len = 16; break; #endif #if (NGX_HAVE_UNIX_DOMAIN) case AF_UNIX: off = offsetof(struct sockaddr_un, sun_path); len = sizeof(saun->sun_path); break; #endif default: /* AF_INET */ off = offsetof(struct sockaddr_in, sin_addr); len = 4; break; } p = lsopt->u.sockaddr_data + off; addr = port->addrs.elts; for (i = 0; i < port->addrs.nelts; i++) { if (ngx_memcmp(p, addr[i].opt.u.sockaddr_data + off, len) != 0) { continue; } /* the address is already in the address list */ // 新加入的地址已经在地址列表中存在了 // 将新的虚拟主机信息加入到这个地址的虚拟主机列表中 if (ngx_http_add_server(cf, cscf, &addr[i]) != NGX_OK) { return NGX_ERROR; } /* preserve default_server bit during listen options overwriting */ default_server = addr[i].opt.default_server; #if (NGX_HTTP_SSL) ssl = lsopt->ssl || addr[i].opt.ssl; #endif #if (NGX_HTTP_SPDY) spdy = lsopt->spdy || addr[i].opt.spdy; #endif if (lsopt->set) { if (addr[i].opt.set) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "duplicate listen options for %s", addr[i].opt.addr); return NGX_ERROR; } addr[i].opt = *lsopt; } /* check the duplicate "default" server for this address:port */ if (lsopt->default_server) { if (default_server) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "a duplicate default server for %s", addr[i].opt.addr); return NGX_ERROR; } default_server = 1; addr[i].default_server = cscf; } addr[i].opt.default_server = default_server; #if (NGX_HTTP_SSL) addr[i].opt.ssl = ssl; #endif #if (NGX_HTTP_SPDY) addr[i].opt.spdy = spdy; #endif return NGX_OK; } /* add the address to the addresses list that bound to this port */ // 添加新地址信息到port的地址列表中 return ngx_http_add_address(cf, cscf, port, lsopt); } // }}}

 

 

这个函数实现了虚拟主机的解析,对于曾经解析过的地址,即虚拟主机配置,调用 ngx_http_add_server 加入到虚拟主机列表中

 

虚拟主机配置添加 -- ngx_http_add_server

/* add the server core module configuration to the address:port */ // static ngx_int_t // ngx_http_add_server(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, // ngx_http_conf_addr_t *addr) // 将地址加入虚拟主机列表 {{{ static ngx_int_t ngx_http_add_server(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, ngx_http_conf_addr_t *addr) { ngx_uint_t i; ngx_http_core_srv_conf_t **server; if (addr->servers.elts == NULL) { if (ngx_array_init(&addr->servers, cf->temp_pool, 4, sizeof(ngx_http_core_srv_conf_t *)) != NGX_OK) { return NGX_ERROR; } } else { server = addr->servers.elts; for (i = 0; i < addr->servers.nelts; i++) { if (server[i] == cscf) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "a duplicate listen %s", addr->opt.addr); return NGX_ERROR; } } } server = ngx_array_push(&addr->servers); if (server == NULL) { return NGX_ERROR; } *server = cscf; return NGX_OK; } // }}}

 

这个函数将解析到的虚拟主机信息存到了 ngx_http_conf_addr_t 结构的 servers 域中

 

添加地址信息到 port 的地址列表中 -- ngx_http_add_address

/* * add the server address, the server names and the server core module * configurations to the port list */ // static ngx_int_t // ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, // ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt) // 将地址添加到配置结构的 ports 数组中 {{{ static ngx_int_t ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt) { ngx_http_conf_addr_t *addr; if (port->addrs.elts == NULL) { if (ngx_array_init(&port->addrs, cf->temp_pool, 4, sizeof(ngx_http_conf_addr_t)) != NGX_OK) { return NGX_ERROR; } } #if (NGX_HTTP_SPDY && NGX_HTTP_SSL \ && !defined TLSEXT_TYPE_application_layer_protocol_negotiation \ && !defined TLSEXT_TYPE_next_proto_neg) if (lsopt->spdy && lsopt->ssl) { ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "nginx was built without OpenSSL ALPN or NPN " "support, SPDY is not enabled for %s", lsopt->addr); } #endif addr = ngx_array_push(&port->addrs); if (addr == NULL) { return NGX_ERROR; } addr->opt = *lsopt; addr->hash.buckets = NULL; addr->hash.size = 0; addr->wc_head = NULL; addr->wc_tail = NULL; #if (NGX_PCRE) addr->nregex = 0; addr->regex = NULL; #endif addr->default_server = cscf; addr->servers.elts = NULL; return ngx_http_add_server(cf, cscf, addr); } // }}}

 

 

这段代码相对比较简单,为 ngx_http_conf_addr_t 结构赋值,最终,实现了下面的结构的完全初始化

 

// struct ngx_http_conf_port_t // 配置 port 结构 {{{ typedef struct { ngx_int_t family; in_port_t port; ngx_array_t addrs; /* array of ngx_http_conf_addr_t */ } ngx_http_conf_port_t; // }}}

 

 

// struct ngx_http_conf_addr_t // 配置地址结构 {{{ typedef struct { ngx_http_listen_opt_t opt; ngx_hash_t hash; ngx_hash_wildcard_t *wc_head; ngx_hash_wildcard_t *wc_tail; #if (NGX_PCRE) ngx_uint_t nregex; ngx_http_server_name_t *regex; #endif /* the default server configuration for this address:port */ ngx_http_core_srv_conf_t *default_server; ngx_array_t servers; /* array of ngx_http_core_srv_conf_t */ } ngx_http_conf_addr_t; // }}}

 

 

// struct ngx_http_core_srv_conf_t // 主机配置结构 {{{ typedef struct { /* array of the ngx_http_server_name_t, "server_name" directive */ ngx_array_t server_names; /* server ctx */ ngx_http_conf_ctx_t *ctx; ngx_str_t server_name; size_t connection_pool_size; size_t request_pool_size; size_t client_header_buffer_size; ngx_bufs_t large_client_header_buffers; ngx_msec_t client_header_timeout; ngx_flag_t ignore_invalid_headers; ngx_flag_t merge_slashes; ngx_flag_t underscores_in_headers; unsigned listen:1; #if (NGX_PCRE) unsigned captures:1; #endif ngx_http_core_loc_conf_t **named_locations; } ngx_http_core_srv_conf_t; // }}}

 

 

// struct ngx_http_server_name_t // 虚拟主机名结构 {{{ 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; // }}}

 

 

在负责解析 server_name 命令的 ngx_http_core_module 的 ngx_http_core_server_name 中,他会将配置中的 server_name 赋值到对应的 ngx_http_server_name_t 中,从而完成上述结构完整的初始化过程

 

四个结构体形成了一个树状结构

 

 

// static ngx_int_t // ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port) // 创建连接结构并初始化 {{{ static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port) { ngx_uint_t i, last, bind_wildcard; ngx_listening_t *ls; ngx_http_port_t *hport; ngx_http_conf_addr_t *addr; addr = port->addrs.elts; last = port->addrs.nelts; /* * If there is a binding to an "*:port" then we need to bind() to * the "*:port" only and ignore other implicit bindings. The bindings * have been already sorted: explicit bindings are on the start, then * implicit bindings go, and wildcard binding is in the end. */ if (addr[last - 1].opt.wildcard) { addr[last - 1].opt.bind = 1; bind_wildcard = 1; } else { bind_wildcard = 0; } i = 0; while (i < last) { if (bind_wildcard && !addr[i].opt.bind) { i++; continue; } ls = ngx_http_add_listening(cf, &addr[i]); if (ls == NULL) { return NGX_ERROR; } hport = ngx_pcalloc(cf->pool, sizeof(ngx_http_port_t)); if (hport == NULL) { return NGX_ERROR; } ls->servers = hport; if (i == last - 1) { hport->naddrs = last; } else { hport->naddrs = 1; i = 0; } switch (ls->sockaddr->sa_family) { #if (NGX_HAVE_INET6) case AF_INET6: if (ngx_http_add_addrs6(cf, hport, addr) != NGX_OK) { return NGX_ERROR; } break; #endif default: /* AF_INET */ if (ngx_http_add_addrs(cf, hport, addr) != NGX_OK) { return NGX_ERROR; } break; } addr++; last--; } return NGX_OK; } // }}}

 

 






技术帖      配置文件      config      龙潭书斋      nginx      server      opensource      conf      listen      http      source      virtualhost      host      address      addr      listening      解析     


京ICP备15018585号