The listener port belongs to the server virtual host, which is determined by the listen configuration item within the server{} block.
In other words, the port that the virtual host is listening to is defined within the server{} block configuration item.
When you process configuration items in the configuration file HTTP block at the main level. Each HTTP module calls create_main_conf, Create_srv_conf, create_loc_conf three methods to build three structures. Used to store configuration items in HTTP blocks, server blocks, and location blocks, respectively.
The Ngx_http_core_module is an HTTP module. So it calls the Ngx_http_core_create_main_conf method within the ngx_http_module_t interface to create a struct that stores the main level configuration item, such as the following:
static void *ngx_http_core_create_main_conf (ngx_conf_t *cf) { ngx_http_core_main_conf_t *cmcf; .... return CMCF;}
it can be seen from the code that a ngx_http_core_main_conf_t struct is created to store configuration items. This struct defines such as the following:
typedef struct { .... ngx_array_t *ports; /* Save All Ports listening in HTTP block configuration item */ ...} ngx_http_core_main_conf_t;
The ports members have all the ports they need to listen to.
Each port is represented by a struct 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;
The port member saves the port to listen on, while the Addrs member holds a series of IP addresses, each of which is represented by a ngx_http_conf_addr_t and each IP address is bound to a port. Like what:
- 127.0.0.1:8000
- 173.39.160.51:8000
Such Suppose a machine has multiple IPs. On the same time to listen to the port of these IPs. Assuming that the configuration item is directly listen 80, it is equivalent to listening to all the addresses under that port by default, namely *.80.
Here's another look at the definition of 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;
Here's a look at the servers array, which associates the listening port with the server{} virtual host. What do you mean? If the corresponding port of ngx_http_conf_addr_t is 8080, and the servers member includes virtual host A and virtual Host B, there is a listen 8080 configuration item in the two block configuration items.
Other words. The servers array in each listener address ngx_http_conf_addr_t is associated with the corresponding server{} virtual host for the listener address.
The total diagram is for example the following:
From the relationship can be derived such as the following facts:
- Server A virtual host listens for two ports. *.80 and 127.0.0.1:8000, respectively.
- The Server B virtual host listens on three ports. Each is *.8080, *.80, 173.39.160.51:8000
References: "In-depth understanding Nginx" p367-p369.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
"Nginx" Monitor port management