首页 > 代码库 > 【Nginx】监听端口的管理
【Nginx】监听端口的管理
监听端口属于server虚拟主机,由server{}块内的listen配置项决定。
在处理配置文件http块内main级别的配置项时,每个HTTP模块都会调用create_main_conf、create_srv_conf、create_loc_conf三个方法建立三个结构体,用来分别存储http块、server块、location块内的配置项。ngx_http_core_module是HTTP模块,所以它会调用ngx_http_module_t接口内的ngx_http_core_create_main_conf方法创建存储main级别配置项的结构体,函数如下:
static void * ngx_http_core_create_main_conf(ngx_conf_t *cf) { ngx_http_core_main_conf_t *cmcf; .... return cmcf; }
从代码可以看出创建了一个ngx_http_core_main_conf_t结构体用于存储配置项。此结构体定义如下:
typedef struct { .... ngx_array_t *ports; /* 保存http块配置项内监听的所有端口 */ .... } ngx_http_core_main_conf_t;
其中的ports成员就保存着所有需要监听的端口。每一个端口用结构体ngx_http_conf_port_t表示
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;
port成员就保存要监听的端口,而addrs成员则保存有一系列的IP地址,每个地址用一个ngx_http_conf_addr_t表示,每一个IP地址都与一个端口绑定,例如:
- 127.0.0.1:8000
- 173.39.160.51:8000
这样,如果一台机器有多个IP,就能够同时监听这些IP的端口了。如果配置项直接就是listen 80,那么相当于默认监听该端口下的所有地址,即*.80。
下面再来看看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;
这里关注servers数组,它把监听的端口与server{}虚拟主机关联起来了。什么意思呢?假如ngx_http_conf_addr_t对应的端口为8080,且servers成员包含虚拟主机A和虚拟主机B,那么在这两个块配置项中就存在listen 8080这个配置项。
参考:
《深入理解Nginx》 P367-P369.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。