location的配置过程以及http流程中的使用情况
2016-09-22 16:54
127 查看
先看看ngx_http_init_listening,在系列文章中已经提到过这个函数,他的第三个参数port是一个结构体,定义如下
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;
前面2个成员变量的含义不言自明,第三个变量addrs是一个数组,填写的是什么呢,是一个叫做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;
他有2个比较重要的成员,opt包含了真实的ip地址,default_server指向这个地址端口对应的配置文件中的默认server{}配置块,servers则指向匹配的所有server块。
然后调用定义一个hport,并调用ngx_http_add_addrs把addrs数组的值传递给hport的addrs成员,特别是把default_server传递过来。最后,hport会指向ls->servers,这个值会在ngx_http_init_connection里面用到。
跳转到这个函数,看看他的具体代码:
hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
if (hc == NULL) {
ngx_http_close_connection(c);
return;
}
c->data = hc;
/* find the server configuration for the address:port */
port = c->listening->servers;
首先根据c->listening->servers找到port,并搜索port->addrs中与c->local_sockaddr相等的地址,然后把他的成员conf传递给hc->addr_conf,hc是一个指向c->data的ngx_http_connect_t的结构体指针,再看下面这句代码:
hc->conf_ctx = hc->addr_conf->default_server->ctx;
hc->conf_ctx 的定义如下
typedef struct {
void **main_conf;
void **srv_conf;
void **loc_conf;
} ngx_http_conf_ctx_t;
这样,hc包含了一个ngx_connection_t对应的所有ngx_http_core_srv_conf_t和ngx_http_core_loc_conf_t。
最后在ngx_http_create_request中,这些指针会传递给ngx_http_request_t的成员:
r->main_conf = hc->conf_ctx->main_conf;
r->srv_conf = hc->conf_ctx->srv_conf;
r->loc_conf = hc->conf_ctx->loc_conf;
每个独立http请求都会通过NGX_HTTP_FIND_CONFIG_PHASE这个阶段,对应的函数是:ngx_http_core_find_config_phase
因为location是放在二叉树结构中,该函数最重要的动作就是递归调用ngx_http_core_find_location,通过ngx_http_regex_exec来匹配r->uri。如果匹配成功就返回,否则继续递归调用,
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;
前面2个成员变量的含义不言自明,第三个变量addrs是一个数组,填写的是什么呢,是一个叫做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;
他有2个比较重要的成员,opt包含了真实的ip地址,default_server指向这个地址端口对应的配置文件中的默认server{}配置块,servers则指向匹配的所有server块。
然后调用定义一个hport,并调用ngx_http_add_addrs把addrs数组的值传递给hport的addrs成员,特别是把default_server传递过来。最后,hport会指向ls->servers,这个值会在ngx_http_init_connection里面用到。
跳转到这个函数,看看他的具体代码:
hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
if (hc == NULL) {
ngx_http_close_connection(c);
return;
}
c->data = hc;
/* find the server configuration for the address:port */
port = c->listening->servers;
首先根据c->listening->servers找到port,并搜索port->addrs中与c->local_sockaddr相等的地址,然后把他的成员conf传递给hc->addr_conf,hc是一个指向c->data的ngx_http_connect_t的结构体指针,再看下面这句代码:
hc->conf_ctx = hc->addr_conf->default_server->ctx;
hc->conf_ctx 的定义如下
typedef struct {
void **main_conf;
void **srv_conf;
void **loc_conf;
} ngx_http_conf_ctx_t;
这样,hc包含了一个ngx_connection_t对应的所有ngx_http_core_srv_conf_t和ngx_http_core_loc_conf_t。
最后在ngx_http_create_request中,这些指针会传递给ngx_http_request_t的成员:
r->main_conf = hc->conf_ctx->main_conf;
r->srv_conf = hc->conf_ctx->srv_conf;
r->loc_conf = hc->conf_ctx->loc_conf;
每个独立http请求都会通过NGX_HTTP_FIND_CONFIG_PHASE这个阶段,对应的函数是:ngx_http_core_find_config_phase
因为location是放在二叉树结构中,该函数最重要的动作就是递归调用ngx_http_core_find_location,通过ngx_http_regex_exec来匹配r->uri。如果匹配成功就返回,否则继续递归调用,
相关文章推荐
- ueditor1.4.3配置过程(包含单独上传文件以及图片的使用),ueditor1.4.3上传配置(转 http://www.bkjia.com/webzh/1001016.html)
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- sqoop配置以及使用过程小结
- Android系统Recovery工作原理之使用update.zip升级过程---updater-script脚本语法简介以及执行流程(转)
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- 【郭林专刊】如何使用TortoiseSVN(例如远程维护代码),请详细说明它的使用过程,包括它的配置,以及使用条件
- 使用cMake跨iOS平台编译过程以及配置
- ueditor1.4.3配置过程(包含单独上传文件以及图片的使用)
- Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- 在 IIS 6 和 IIS 7中配置Https,设置WCF同时支持HTTP和HTPPS,以及使用HttpWebRequest和HttpClient调用HttpS
- Android系统Recovery工作原理之使用update.zip升级过程---updater-script脚本语法简介以及执行流程
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- 在静态库下使用MFC和共享DLL下使用MFC 这两种情况 opencv的配置,以及配置opencv配置中容易出现的问题
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- 在静态库下使用MFC和共享DLL下使用MFC 这两种情况 opencv的配置,以及配置opencv配置中容易出现的问题