nignx解析配置文件时,当遇到一下配置时,会做哪些事情?
upstream test.balancer.com {
1.1.1.1:80;
2.2.2.2:80;
}
一、 存储upstream配置的三大结构体
首先需要解决upstream配置的存储问题,nginx是通过ngx_http_upstream_main_conf_t,ngx_http_upstream_srv_conf_t,ngx_http_upstream_server_t 三个结构体来存储upstream配置的。
其中ngx_http_upstream_main_conf_t的定义如下:
typedef struct {
ngx_hash_t headers_in_hash;
ngx_array_t upstreams; //每一个upstream都存储upstreams数组里。
/* ngx_http_upstream_srv_conf_t */
} ngx_http_upstream_main_conf_t;
其中的upstream成员是一个数组,数组中的每个元素又是ngx_http_upstream_srv_conf_t类型的。
ngx_http_upstream_srv_conf_t定义如下:
struct ngx_http_upstream_srv_conf_s {
ngx_http_upstream_peer_t peer;
void **srv_conf;
ngx_array_t *servers; /* ngx_http_upstream_server_t */
ngx_uint_t flags;
ngx_str_t host;
u_char *file_name;
ngx_uint_t line;
in_port_t port;
ngx_uint_t no_port; /* unsigned no_port:1 */
#if (NGX_HTTP_UPSTREAM_ZONE)
ngx_shm_zone_t *shm_zone;
#endif
};
其中的servers字段又保存了该upstream配置中每个server的详细信息,包括ip、weight,max_conns,down等信息。
typedef struct {
ngx_str_t name;
ngx_addr_t *addrs;
ngx_uint_t naddrs;
ngx_uint_t weight;
ngx_uint_t max_conns;
ngx_uint_t max_fails;
time_t fail_timeout;
ngx_msec_t slow_start;
ngx_uint_t down;
unsigned backup:1;
NGX_COMPAT_BEGIN(6)
NGX_COMPAT_END
} ngx_http_upstream_server_t;