上一篇提到ngx_http_block(或者说http块指令)是Nginx中HTTP模块的入口,这一篇学习下ngx_http_block这个指令回调中做了哪些工作。
从以下代码中,我们可以看出,ngx_http_block主要做了如下工作:
1. 创建HTTP配置上下文
2.对http指令块进行配置解析
3.初始化HTTP各处理阶段
4.对HTTP监听的端口、地址、虚拟主机名称进行合并整理,然后创建对应的监听套接字结构体
/* http/ngx_http_config.h */
/* HTTP的配置以http块指令作为开始, 里面可以包含一些行指令(main层次)和多个server/location块指令;
* server块指令可以包含一些行指令(server层次)和多个location块指令;
* location块指令可以包含一些行指令(location层次);
* main、server和location三个层次的指令可以重复, 那么后面的指令值会覆盖前面的
*/
typedef struct { // HTTP配置上下文结构体定义
void **main_conf; // main层次配置上下文数组
void **srv_conf; // server层次配置上下文数组
void **loc_conf; // location层次配置上下文数组
} ngx_http_conf_ctx_t;
/* http/ngx_http_core_module.h */
typedef struct { // HTTP监听结构体定义
in_addr_t addr; // 监听地址
in_port_t port; // 监听端口
int family; // 地址族
ngx_str_t file_name;
int line;
unsigned default_server:1; // 默认虚拟主机标志位
} ngx_http_listen_t;
typedef struct { // HTTP请求处理按阶段进行, 这里是处理阶段结构体定义
ngx_array_t handlers; // 阶段的处理函数指针数组
ngx_int_t type; // 期望的处理函数返回值, NGX_OK/NGX_DECLINED
} ngx_http_phase_t;
typedef struct { // ngx_http_core_module模块的main层次配置上下文结构体定义
ngx_array_t servers; // server数组, 元素类型为ngx_http_core_srv_conf_t
ngx_http_phase_t phases[NGX_HTTP_LAST_PHASE]; // 处理阶段数组, NGX_HTTP_LAST_PHASE为阶段枚举类型的结束标记, 因此这个数组的元素个数即为有效阶段个数
ngx_array_t index_handlers;
size_t max_server_name_len; // 虚拟主机名称的最大长度
} ngx_http_core_main_conf_t;
typedef struct { // ngx_http_core_module模块的server层次配置上下文结构体定义
ngx_array_t locations; // locations数组, 元素类型为ngx_http_core_loc_conf_t
ngx_array_t listen; // ngx_http_listen_t数组, 监听的<addr:port>
ngx_array_t server_names; // ngx_http_server_name_t数组, 虚拟主机名称
ngx_http_conf_ctx_t *ctx; // HTTP配置上下文
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 post_accept_timeout;
ngx_msec_t client_header_timeout;
ngx_uint_t restrict_host_names;
} ngx_http_core_srv_conf_t;
typedef struct ngx_http_core_loc_conf_s ngx_http_core_loc_conf_t;
struct ngx_http_core_loc_conf_s {
ngx_str_t name;
#if (HAVE_PCRE)
ngx_regex_t *regex;
#endif
unsigned exact_match:1;
unsigned auto_redirect:1;
unsigned alias:1;
/* array of inclusive ngx_http_core_loc_conf_t */
ngx_array_t locations;
/* pointer to the modules' loc_conf */
void **loc_conf ;
ngx_http_handler_pt handler;
ngx_str_t root; /* root, alias */
ngx_array_t *types;
ngx_str_t default_type;
size_t client_max_body_size; /* client_max_body_size */
size_t client_body_buffer_size; /* client_body_buffer_size */
size_t send_lowat; /* send_lowat */
size_t postpone_output; /* postpone_output */
size_t limit_rate; /* limit_rate */
ngx_msec_t client_body_timeout; /* client_body_timeout */
ngx_msec_t send_timeout; /* send_timeout */
ngx_msec_t keepalive_timeout; /* keepalive_timeout */
ngx_msec_t lingering_time; /* lingering_time */
ngx_msec_t lingering_timeout; /* lingering_timeout */
time_t keepalive_header; /* keepalive_timeout */
ngx_flag_t sendfile; /* sendfile */
ngx_flag_t tcp_nopush; /* tcp_nopush */
ngx_flag_t reset_timedout_connection; /* reset_timedout_connection */
ngx_flag_t msie_padding; /* msie_padding */
ngx_array_t *error_pages; /* error_page */
ngx_http_cache_hash_t *open_files;
ngx_log_t *err_log;
ngx_http_core_loc_conf_t *prev_location;
};
typedef struct { // 虚拟主机名称结构体定义
ngx_str_t name; // 虚拟主机名称
ngx_http_core_srv_conf_t *core_srv_conf; // 虚拟主机名称的配置
} ngx_http_server_name_t;
typedef struct { // HTTP监听地址结构体定义
in_addr_t addr; // IPv4地址
ngx_array_t names; // ngx_http_server_name_t数组, 虚拟主机名称
ngx_http_core_srv_conf_t *core_srv_conf; // 当default_server为1时, 这里指向该地址下的server层次配置上下文,
// 即默认虚拟主机的配置
unsigned default_server:1; // 默认虚拟主机标志位
} ngx_http_in_addr_t;
typedef struct { // HTTP监听端口结构体定义
in_port_t port; // 端口号
ngx_str_t port_text; // 端口的十进制字符串
ngx_array_t addrs; // ngx_http_in_addr_t数组, 与端口绑定的地址
} ngx_http_in_port_t;
/* http/ngx_http.c */
/* 对location进行合并(location类似URL)
*/
static char *ngx_http_merge_locations(ngx_conf_t *cf,
ngx_array_t *locations,
void **loc_conf,
ngx_http_m