Nginx監聽端口管理
每監聽一個TCP端口,都將使用一個獨立的ngx_http_conf_port_t結構體表示。
ngx_http_conf_port_t
typedef struct {
//socket地址家族
ngx_int_t family;
//監聽端口
in_port_t port;
//監聽端口下對應着的所有ngx_http_conf_addr_t地址
ngx_array_t addrs;
} ngx_http_conf_port_t;
這個ngx_http_conf_port_t結構體保存在ngx_http_core_main_conf_t結構體中:
typedef struct {
//存放該http{}配置塊下監聽的所有ngx_http_port_t端口
ngx_array_t *port;
...
} ngx_http_core_main_conf_t;
由於一個http{}中可能監聽多個端口,所以這裏是動態數組的形式存儲port。對於一個port,可能會監聽多個IP地址,這些元素都保存在ngx_http_conf_port_t結構體中的addrs成員中。每個元素都是如下結構:
ngx_http_conf_addr_t
typedef struct {
// 監聽套接字的各種屬性
ngx_http_listen_opt_t opt;
// 以下三個散列表用於確定到底使用哪個server{}虛擬主機下的配置來處理端口上的新連接
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_http_core_srv_conf_t結構體,用於將監聽端口和server{}虛擬主機關聯起來
ngx_array_t servers;
} ngx_http_conf_addr_t;
對於每一個監聽地址ngx_http_conf_addr_t,都有一個ngx_listening_t與其對應。