这篇文章主要介绍了Nginx中的HTTP协议相关模块怎么配置的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Nginx中的HTTP协议相关模块怎么配置文章都会有所收获,下面我们一起来看看吧。
要理解 http 模块配置解析的过程,首先需要对 nginx 的配置文件结构做一个了解
nginx 的配置文件是用树状结构组织的,每个 ngx_core_module 作为根统领着其下的所有配置项
而如下图所示,http 模块的配置被分成了 main、server、location 三层
整个 nginx 配置解析的过程其实就是这棵树的深度遍历过程
而遍历 http 子树的函数就是下面要介绍的 ngx_http_block
配置文件解析 -- http 配置块
当我们需要使用 http 模块的时候,我们需要在配置文件中加入 http 配置块:
http { // http 配置块 {{{
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request"
'
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8001;
server_name localhost;
#autoindex on;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /var/www/;
index index.html index.htm index.php;
在 http 配置块中,我们配置了 http 连接相关的信息,http 框架也正是从这里启动的.
在 nginx 初始化的过程中,执行了 ngx_init_cycle 函数,其中进行了配置文件解析,调用了 ngx_conf_parse 函数
配置文件解析
函数 ngx_conf_handler 根据配置项的 command 调用了对应的 set 回调函数
// static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last)
// 配置项解析 {{{
static ngx_int_t
ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last)
{
char *rv;
void *conf, **confp;
ngx_uint_t i, found;
ngx_str_t *name;
ngx_command_t *cmd;
name = cf->args->elts;
found = 0;
for (i = 0; ngx_modules[i]; i++) {
cmd = ngx_modules[i]->commands;
if (cmd == null) {
continue;
}
for ( /* void */ ; cmd->name.len; cmd++) {
if (name->len != cmd->name.len) {
continue;
}
if (ngx_strcmp(name->data, cmd->name.data) != 0) {
continue;
}
阅读各模块的 ngx_command_t 命令配置结构,可以找到:
// static ngx_command_t ngx_http_commands
// http 模块命令结构 {{{
static ngx_command_t ngx_http_commands[] = {
{ ngx_string("http"),
ngx_main_conf|ngx_conf_block|ngx_conf_noargs,
ngx_http_block,
0,
0,
null },
ngx_null_command
}; // }}}
http 配置块解析 -- ngx_http_block
在解析到 http 配置块时,执行了对应的 set 回调函数 ngx_http_block
static char *
ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *rv;
ngx_uint_t mi, m, s;
ngx_conf_t pcf;
ngx_http_module_t *module;
ngx_http_conf_ctx_t *ctx;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t **cscfp;
ngx_http_core_main_conf_t *cmcf;
/* the main http context */
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
if (ctx == null) {
return ngx_conf_error;
}
// 创建 http 配置结构,保存所有 http 模块的配置信息
*(ngx_http_conf_ctx_t **) conf = ctx;
/* count the number of the http modules and set up their indices */
ngx_http_max_module = 0;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != ngx_http_module) {
continue;
}
// 重新设定 http 模块序号
ngx_modules[m]->ctx_index = ngx_http_max_module++;
}
/* the http main_conf context, it is the same in the all http contexts */
ctx->main_conf = ngx_pcalloc(cf->pool,
sizeof(void *) * ngx_http_max_module);
if (ctx->main_conf == null) {
return ngx_conf_error;
}
/*
* the http null srv_conf context, it is used to merge
* the server{}s' srv_conf's
*/
ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->srv_conf == null) {
return ngx_conf_error;
}
/*
* the http null loc_conf context, it is used to merge
* the server{}s' loc_conf's
*/
ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->loc_conf == null) {
return ngx_conf_error;
}
/*
* create the main_conf's, the null srv_conf's, and the null loc_conf's
* of the all http modules
*/
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != ngx_http_module) {
continue;
}
module = ngx_modules[m]->ctx;
mi = ngx_modules[m]->ctx_index;
if (module->create_main_conf) {
// 调用每个模块的 create_main_conf 回调函数
// 创建自己的 main_conf
//
// ngx_http_core_module ngx_http_core_create_main_conf
// ngx_http_log_module ngx_http_log_create_main_conf
// ngx_http_upstream_module ngx_http_upstream_create_main_conf
// ngx_http_map_module ngx_http_map_create_conf
// ngx_http_ssi_filter_module ngx_http_ssi_create_main_conf
// ngx_http_charset_filter_module ngx_http_charset_create_main_conf
ctx->main_conf[mi] = module->create_main_conf(cf);
if (ctx->main_conf[mi] == null) {
return ngx_conf_error;
}
}
if (module->create_srv_conf) {
// 调用每个模块的 create_srv_conf 回调函数
// 创建自己的 srv_conf
//
// ngx_http_core_module ngx_http_core_create_srv_conf
// ngx_http_ssl_module ngx_http_ssl_create_srv_conf
// ngx_http_upstream_hash_module ngx_http_upstream_hash_create_conf
// ngx_http_upstream_least_conn_module ngx_http_upstream_least_conn_create_conf
// ngx_http_upstream_keepalive_module ngx_http_upstream_keepalive_create_conf
ctx->srv_conf[mi] = module->create_srv_conf(cf);
if (ctx->srv_conf[mi] == null) {
return ngx_conf_error;
}
}
if (module->create_loc_conf) {
// 调用每个模块的 create_loc_conf 回调函数
// 创建自己的 loc_conf
//
// ngx_http_core_module ngx_http_core_create_loc_conf
// ngx_http_log_module ngx_http_log_create_loc_conf
// ngx_http_gzip_static_module ngx_http_gzip_static_create_conf
// ngx_http_autoindex_module ngx_http_autoindex_create_loc_conf
// ngx_http_index_module ngx_http_index_create_loc_conf
// ngx_http_auth_basic_module ngx_http_auth_basic_create_loc_conf
// ngx_http_access_module ngx_http_access_create_loc_conf
// ngx_http_limit_conn_module ngx_http_limit_conn_create_conf
// ngx_http_limit_req_module ngx_http_limit_req_create_conf
// ngx_http_referer_module ngx_http_referer_create_conf
// ngx_http_rewrite_module ngx_http_rewrite_create_loc_conf
// ngx_http_proxy_module ngx_http_proxy_create_loc_conf
// ngx_http_fastcgi_module ngx_http_fastcgi_create_loc_conf
// ngx_http_uwsgi_module ngx_http_uwsgi_create_loc_conf
// ngx_http_scgi_module ngx_http_scgi_create_loc_conf
// ngx_http_memcached_module ngx_http_memcached_create_loc_conf
// ngx_http_browser_module ngx_http_browser_create_conf
// ngx_http_gzip_filter_module ngx_http_gzip_create_conf
// ngx_http_ssi_filter_module ngx_http_ssi_create_loc_conf
// ngx_http_charset_filter_module ngx_http_charset_create_loc_conf
// ngx_http_userid_filter_module ngx_http_userid_create_conf
// ngx_http_headers_filter_module ngx_http_headers_create_conf
// ngx_http_copy_filter_module ngx_http_copy_filter_create_conf
ctx->loc_conf[mi] = module->create_loc_conf(cf);
if (ctx->loc_conf[mi] == null) {
return ngx_conf_error;
}
}
}
pcf = *cf;
cf->ctx = ctx;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != ngx_http_module) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->preconfiguration) {
// 调用每个模块的 preconfiguration 回调函数
//
// ngx_http_core_module ngx_http_core_preconfiguration
// ngx_http_upstream_module ngx_http_upstream_add_variables
// ngx_http_ssl_module ngx_http_ssl_add_variables
// ngx_http_proxy_module ngx_http_proxy_add_variables
// ngx_http_fastcgi_module ngx_http_fastcgi_add_variables
// ngx_http_browser_module ngx_http_browser_add_variable
// ngx_http_stub_status_module ngx_http_stub_status_add_variables
// ngx_http_gzip_filter_module ngx_http_gzip_add_variables
// ngx_http_ssi_filter_module ngx_http_ssi_preconfiguration
// ngx_http_userid_filter_module ngx_http_userid_add_variables
if (module->preconfiguration(cf) != ngx_ok) {
return ngx_conf_error;
}
}
}
/* parse inside the http{} block */
cf->module_type = ngx_http_module;
cf->cmd_type = ngx_http_main_conf;
rv = ngx_conf_parse(cf, null);
if (rv != ngx_conf_ok) {
goto failed;
}
/*
* init http{} main_conf's, merge the server{}s' srv_conf's
* and its location{}s' loc_conf's
*/
cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
cscfp = cmcf->servers.elts;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != ngx_http_module) {
continue;
}
module = ngx_modules[m]->ctx;
mi = ngx_modules[m]->ctx_index;
/* init http{} main_conf's */
if (module->init_main_conf) {
// 调用每个模块的 init_main_conf 回调函数
// 初始化自己的 main_conf
//
// ngx_http_core_module ngx_http_core_init_main_conf
// ngx_http_upstream_module ngx_http_upstream_init_main_conf
// ngx_http_ssi_filter_module ngx_http_ssi_init_main_conf
rv = module->init_main_conf(cf, ctx->main_conf[mi]);
if (rv != ngx_conf_ok) {
goto failed;
}
}
// 合并同名配置项
rv = ngx_http_merge_servers(cf, cmcf, module, mi);
if (rv != ngx_conf_ok) {
goto failed;
}
}
/* create location trees */
for (s = 0; s < cmcf->servers.nelts; s++) {
clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
if (ngx_http_init_locations(cf, cscfp[s], clcf) != ngx_ok) {
return ngx_conf_error;
}
if (ngx_http_init_static_location_trees(cf, clcf) != ngx_ok) {
return ngx_conf_error;
}
}
if (ngx_http_init_phases(cf, cmcf) != ngx_ok) {
return ngx_conf_error;
}
if (ngx_http_init_headers_in_hash(cf, cmcf) != ngx_ok) {
return ngx_conf_error;
}
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != ngx_http_module) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->postconfiguration) {
// 调用每个 http 模块的 postconfiguration 回调函数
//
// ngx_http_log_module ngx_http_log_init
// ngx_http_static_module ngx_http_static_init
// ngx_http_gzip_static_module ngx_http_gzip_static_init
// ngx_http_autoindex_module ngx_http_autoindex_init
// ngx_http_index_module ngx_http_index_init
// ngx_http_auth_basic_module ngx_http_auth_basic_init
// ngx_http_access_module ngx_http_access_init
// ngx_http_limit_conn_module ngx_http_limit_conn_init
// ngx_http_limit_req_module ngx_http_limit_req_init
// ngx_http_rewrite_module ngx_http_rewrite_init
// ngx_http_ssl_module ngx_http_ssl_init
// ngx_http_write_filter_module ngx_http_write_filter_init
// ngx_http_header_filter_module ngx_http_header_filter_init
// ngx_http_chunked_filter_module ngx_http_chunked_filter_init
// ngx_http_range_body_filter_module ngx_http_range_body_filter_init
// ngx_http_gzip_filter_module ngx_http_gzip_filter_init
// ngx_http_postpone_filter_module ngx_http_postpone_filter_init
// ngx_http_ssi_filter_module ngx_http_ssi_filter_init
// ngx_http_charset_filter_module ngx_http_charset_postconfiguration
// ngx_http_userid_filter_module ngx_http_userid_init
// ngx_http_headers_filter_module ngx_http_headers_filter_init
// ngx_http_copy_filter_module ngx_http_copy_filter_init
// ngx_http_range_body_filter_module ngx_http_range_body_filter_init
// ngx_http_not_modified_filter_module ngx_http_not_modified_filter_init
if (module->postconfiguration(cf) != ngx_ok) {
return ngx_conf_error;
}
}
}
// 初始化所有的变量
// 不仅包括http core模块的变量
// 也包括其他的http模块导出的变量,以及配置文件中使用 set 命令设置的变量
// 这里的初始化包括初始化hash表,以及初始化数组索引
if (ngx_http_variables_init_vars(cf) != ngx_ok) {
return ngx_conf_error;
}
/*
* http{}'s cf->ctx was needed while the configuration merging
* and in postconfiguration process
*/
*cf = pcf;
// 初始化 phase_engine 结构
if (ngx_http_init_phase_handlers(cf, cmcf) != ngx_ok) {
return ngx_conf_error;
}
/* optimize the lists of ports, addresses and server names */
// 创建 http 连接,并设置为监听状态
if (ngx_http_optimize_servers(cf, cmcf, cmcf->ports) != ngx_ok) {
return ngx_conf_error;
}
return ngx_conf_ok;
failed:
*cf = pcf;
return rv;
} // }}}
这个函数中,为所有的 http 模块都分配并创建了配置结构,同时,调用了每个模块相应的初始化回调。
最后,调用 ngx_http_optimize_servers 创建了 http 连接,加入 cycle 的监听数组,并设为监听状态。
nginx 配置文件对 http 模块的配置分为三层:main、sever、location,因此,http 模块上下文 ngx_http_module_t 中定义了以下六个回调函数,用来创建和保存配置信息:
create_main_conf
init_main_conf
create_srv_conf
merge_srv_conf
create_loc_conf
merge_loc_conf
在 ngx_http_block 中,循环调用了所有 ngx_http_module 的这六个回调函数,创建相关的配置结构。
server、location 配置解析 -- ngx_http_core_server、ngx_http_core_location
在调用所有 http 模块的 create_main_conf、create_srv_conf、create_loc_conf 后,所有需要配置结构的模块都完成了配置结构的创建,于是在调用所有模块的 preconfiguration 回调函数后,配置解析工作正式展开
通过调用 ngx_conf_parse 函数,开始了 http 配置块的解析,并通过解析到的命令调用相应的函数
在首个 ngx_http_module ngx_http_core_module 的 ngx_command_t 域中包含了大量的配置指令,它们都是在http{}块中出现的,其中包括两个重要的指令:
{ ngx_string("listen"),
ngx_http_srv_conf|ngx_conf_1more,
ngx_http_core_listen,
ngx_http_srv_conf_offset,
0,
null },
{ ngx_string("server"),
ngx_http_main_conf|ngx_conf_block|ngx_conf_noargs,
ngx_http_core_server,
0,
0,
null },
{ ngx_string("location"),
ngx_http_srv_conf|ngx_http_loc_conf|ngx_conf_block|ngx_conf_take12,
ngx_http_core_location,
ngx_http_srv_conf_offset,
0,
null },
这里配置了 listen、server 与 location 块的解析函数 ngx_http_core_listen、ngx_http_core_server 和 ngx_http_core_location.
server 块解析 -- ngx_http_core_server
// static char *
// ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
// http server 块解析 {{{
static char *
ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
{
char *rv;
void *mconf;
ngx_uint_t i;
ngx_conf_t pcf;
ngx_http_module_t *module;
struct sockaddr_in *sin;
ngx_http_conf_ctx_t *ctx, *http_ctx;
ngx_http_listen_opt_t lsopt;
ngx_http_core_srv_conf_t *cscf, **cscfp;
ngx_http_core_main_conf_t *cmcf;
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
if (ctx == null) {
return ngx_conf_error;
}
http_ctx = cf->ctx;
ctx->main_conf = http_ctx->main_conf;
/* the server{}'s srv_conf */
// 为所有 http 模块分配空间存储 srv_conf
ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->srv_conf == null) {
return ngx_conf_error;
}
/* the server{}'s loc_conf */
// 为所有 http 模块分配空间存储 loc_conf
ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->loc_conf == null) {
return ngx_conf_error;
}
// 循环调用每个模块的 create_srv_conf 与 create_loc_conf 回调,创建配置结构
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->type != ngx_http_module) {
continue;
}
module = ngx_modules[i]->ctx;
if (module->create_srv_conf) {
mconf = module->create_srv_conf(cf);
if (mconf == null) {
return ngx_conf_error;
}
ctx->srv_conf[ngx_modules[i]->ctx_index] = mconf;
}
if (module->create_loc_conf) {
mconf = module->create_loc_conf(cf);
if (mconf == null) {
return ngx_conf_error;
}
ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
}
}
/* the server configuration context */
cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
cscf->ctx = ctx;
cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
cscfp = ngx_array_push(&cmcf->servers);
if (cscfp == null) {
return ngx_conf_error;
}
*cscfp = cscf;
/* parse inside server{} */
pcf = *cf;
cf->ctx = ctx;
cf->cmd_type = ngx_http_srv_conf;
// 解析 server 块配置
rv = ngx_conf_parse(cf, null);
*cf = pcf;
// 如果没有监听任何端口,则监听默认的 80 或 8000端口
if (rv == ngx_conf_ok && !cscf->listen) {
ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
sin = &lsopt.u.sockaddr_in;
sin->sin_family = af_inet;
#if (ngx_win32)
sin->sin_port = htons(80);
#else
sin->sin_port = htons((getuid() == 0) ? 80 : 8000);
#endif
sin->sin_addr.s_addr = inaddr_any;
lsopt.socklen = sizeof(struct sockaddr_in);
lsopt.backlog = ngx_listen_backlog;
lsopt.rcvbuf = -1;
lsopt.sndbuf = -1;
#if (ngx_have_setfib)
lsopt.setfib = -1;
#endif
#if (ngx_have_tcp_fastopen)
lsopt.fastopen = -1;
#endif
lsopt.wildcard = 1;
(void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr,
ngx_sockaddr_strlen, 1);
if (ngx_http_add_listen(cf, cscf, &lsopt) != ngx_ok) {
return ngx_conf_error;
}
}
return rv;
} // }}}
location 块解析 -- ngx_http_core_location
// static char *
// ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
// location 块配置解析 {{{
static char *
ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
{
char *rv;
u_char *mod;
size_t len;
ngx_str_t *value, *name;
ngx_uint_t i;
ngx_conf_t save;
ngx_http_module_t *module;
ngx_http_conf_ctx_t *ctx, *pctx;
ngx_http_core_loc_conf_t *clcf, *pclcf;
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
if (ctx == null) {
return ngx_conf_error;
}
pctx = cf->ctx;
ctx->main_conf = pctx->main_conf;
ctx->srv_conf = pctx->srv_conf;
ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->loc_conf == null) {
return ngx_conf_error;
}
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->type != ngx_http_module) {
continue;
}
module = ngx_modules[i]->ctx;
if (module->create_loc_conf) {
ctx->loc_conf[ngx_modules[i]->ctx_index] =
module->create_loc_conf(cf);
if (ctx->loc_conf[ngx_modules[i]->ctx_index] == null) {
return ngx_conf_error;
}
}
}
clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
clcf->loc_conf = ctx->loc_conf;
value = cf->args->elts;
if (cf->args->nelts == 3) {
len = value[1].len;
mod = value[1].data;
name = &value[2];
if (len == 1 && mod[0] == '=') {
clcf->name = *name;
clcf->exact_match = 1;
} else if (len == 2 && mod[0] == '^' && mod[1] == '~') {
clcf->name = *name;
clcf->noregex = 1;
} else if (len == 1 && mod[0] == '~') {
if (ngx_http_core_regex_location(cf, clcf, name, 0) != ngx_ok) {
return ngx_conf_error;
}
} else if (len == 2 && mod[0] == '~' && mod[1] == '*') {
if (ngx_http_core_regex_location(cf, clcf, name, 1) != ngx_ok) {
return ngx_conf_error;
}
} else {
ngx_conf_log_error(ngx_log_emerg, cf, 0,
"invalid location modifier \"%v\"", &value[1]);
return ngx_conf_error;
}
} else {
name = &value[1];
if (name->data[0] == '=') {
clcf->name.len = name->len - 1;
clcf->name.data = name->data + 1;
clcf->exact_match = 1;
} else if (name->data[0] == '^' && name->data[1] == '~') {
clcf->name.len = name->len - 2;
clcf->name.data = name->data + 2;
clcf->noregex = 1;
} else if (name->data[0] == '~') {
name->len--;
name->data++;
if (name->data[0] == '*') {
name->len--;
name->data++;
if (ngx_http_core_regex_location(cf, clcf, name, 1) != ngx_ok) {
return ngx_conf_error;
}
} else {
if (ngx_http_core_regex_location(cf, clcf, name, 0) != ngx_ok) {
return ngx_conf_error;
}
}
} else {
clcf->name = *name;
if (name->data[0] == '@') {
clcf->named = 1;
}
}
}
pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
if (pclcf->name.len) {
/* nested location */
#if 0
clcf->prev_location = pclcf;
#endif
if (pclcf->exact_match) {
ngx_conf_log_error(ngx_log_emerg, cf, 0,
"location \"%v\" cannot be inside "
"the exact location \"%v\"",
&clcf->name, &pclcf->name);
return ngx_conf_error;
}
if (pclcf->named) {
ngx_conf_log_error(ngx_log_emerg, cf, 0,
"location \"%v\" cannot be inside "
"the named location \"%v\"",
&clcf->name, &pclcf->name);
return ngx_conf_error;
}
if (clcf->named) {
ngx_conf_log_error(ngx_log_emerg, cf, 0,
"named location \"%v\" can be "
"on the server level only",
&clcf->name);
return ngx_conf_error;
}
len = pclcf->name.len;
#if (ngx_pcre)
if (clcf->regex == null
&& ngx_filename_cmp(clcf->name.data, pclcf->name.data, len) != 0)
#else
if (ngx_filename_cmp(clcf->name.data, pclcf->name.data, len) != 0)
#endif
{
ngx_conf_log_error(ngx_log_emerg, cf, 0,
"location \"%v\" is outside location \"%v\"",
&clcf->name, &pclcf->name);
return ngx_conf_error;
}
}
// 将 location 配置加入到 locations 配置链表中
if (ngx_http_add_location(cf, &pclcf->locations, clcf) != ngx_ok) {
return ngx_conf_error;
}
save = *cf;
cf->ctx = ctx;
cf->cmd_type = ngx_http_loc_conf;
rv = ngx_conf_parse(cf, null);
*cf = save;
return rv;
} // }}}
与 server 块解析函数 ngx_http_core_server 类似,他创建了所有模块的 loc_conf,为了防止内外层具有相同指令,在配置赋值完成后,会通过 merge 函数合并到一起。
然而,与 server 块不同,location 块在 location 后面会通过路径或正则表达式指定 location 配置的应用 uri,因此,在 ngx_http_core_location 函数中调用 pcre 进行了 location 命令的解析。
解析完成后调用 ngx_http_add_location 将解析结果加入到 locations 链表中。
// ngx_int_t ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
// ngx_http_core_loc_conf_t *clcf)
// 将 location 配置加入到 locations 配置链表中 {{{
ngx_int_t
ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
ngx_http_core_loc_conf_t *clcf)
{
ngx_http_location_queue_t *lq;
if (*locations == null) {
*locations = ngx_palloc(cf->temp_pool,
sizeof(ngx_http_location_queue_t));
if (*locations == null) {
return ngx_error;
}
ngx_queue_init(*locations);
}
lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
if (lq == null) {
return ngx_error;
}
if (clcf->exact_match
#if (ngx_pcre)
|| clcf->regex
#endif
|| clcf->named || clcf->noname)
{
lq->exact = clcf;
lq->inclusive = null;
} else {
lq->exact = null;
lq->inclusive = clcf;
}
lq->name = &clcf->name;
lq->file_name = cf->conf_file->file.name.data;
lq->line = cf->conf_file->line;
ngx_queue_init(&lq->list);
ngx_queue_insert_tail(*locations, &lq->queue);
return ngx_ok;
} // }}}
配置解析全部完成后的配置结构。
关于“Nginx中的HTTP协议相关模块怎么配置”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Nginx中的HTTP协议相关模块怎么配置”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/2425469/blog/4385146