【nginx流程分析之读取配置文件http模块】

  • http
    • 配置文件
    • 模块定义
    • 结构体定义
      • ngx_http_module_t
      • ngx_http_conf_ctx_t
      • ngx_http_core_loc_conf_t
      • ngx_http_core_srv_conf_t
      • ngx_http_core_main_conf_t
    • 流程分析

http

先看看配置文件中http的定义,地址走/usr/local/nginx/conf/nginx.conf

配置文件

http {include       mime.types;server {listen       80;server_name  localhost;location / {root   html;index  index.html index.htm;}}
}

模块定义

首先看看http的模块的定义

ngx_module_t  ngx_http_module = {NGX_MODULE_V1,&ngx_http_module_ctx,                  /* module context */ngx_http_commands,                     /* module directives */NGX_CORE_MODULE,                       /* module type */NULL,                                  /* init master */NULL,                                  /* init module */NULL,                                  /* init process */NULL,                                  /* init thread */NULL,                                  /* exit thread */NULL,                                  /* exit process */NULL,                                  /* exit master */NGX_MODULE_V1_PADDING
};
static ngx_command_t  ngx_http_commands[] = {{ ngx_string("http"),NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,ngx_http_block, //实现的set方法0,0,NULL },ngx_null_command
};

上面可以看到是命中http命令之后,初始化配置文件的方法ngx_http_block。

结构体定义

ngx_http_module_t

typedef struct {ngx_int_t   (*preconfiguration)(ngx_conf_t *cf);ngx_int_t   (*postconfiguration)(ngx_conf_t *cf);void       *(*create_main_conf)(ngx_conf_t *cf);char       *(*init_main_conf)(ngx_conf_t *cf, void *conf);void       *(*create_srv_conf)(ngx_conf_t *cf);char       *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);void       *(*create_loc_conf)(ngx_conf_t *cf);char       *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);
} ngx_http_module_t;

可以看出来 ngx_http_module_t 都是抽象类的方法,比较类似的第一个参数都是ngx_conf_t的指针

ngx_http_conf_ctx_t

typedef struct {void        **main_conf;void        **srv_conf;void        **loc_conf;
} ngx_http_conf_ctx_t;

比较简单都是 类型为void的二级指针。
然后 main_conf 就是http 里面的公共部分,比如 include,sendfile,keepalive_timeout等等。如图:

然后srv_conf ,就是server 如图:

ngx_http_core_loc_conf_t

其实也就是ngx_http_core_loc_conf_s,看一下ngx_http_core_loc_conf_s的定义,如图


struct ngx_http_core_loc_conf_s {ngx_str_t     name;          /* location name */#if (NGX_PCRE)ngx_http_regex_t  *regex;
#endifunsigned      noname:1;   /* "if () {}" block or limit_except */unsigned      lmt_excpt:1;unsigned      named:1;unsigned      exact_match:1;unsigned      noregex:1;unsigned      auto_redirect:1;
#if (NGX_HTTP_GZIP)unsigned      gzip_disable_msie6:2;unsigned      gzip_disable_degradation:2;
#endifngx_http_location_tree_node_t   *static_locations;
#if (NGX_PCRE)ngx_http_core_loc_conf_t       **regex_locations;
#endif/* pointer to the modules' loc_conf */void        **loc_conf;uint32_t      limit_except;void        **limit_except_loc_conf;ngx_http_handler_pt  handler;/* location name length for inclusive location with inherited alias */size_t        alias;ngx_str_t     root;                    /* root, alias */ngx_str_t     post_action;ngx_array_t  *root_lengths;ngx_array_t  *root_values;ngx_array_t  *types;ngx_hash_t    types_hash;ngx_str_t     default_type;off_t         client_max_body_size;    /* client_max_body_size */off_t         directio;                /* directio */off_t         directio_alignment;      /* directio_alignment */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 */size_t        limit_rate_after;        /* limit_rate_after */size_t        sendfile_max_chunk;      /* sendfile_max_chunk */size_t        read_ahead;              /* read_ahead */size_t        subrequest_output_buffer_size;/* subrequest_output_buffer_size */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 */ngx_msec_t    resolver_timeout;        /* resolver_timeout */ngx_resolver_t  *resolver;             /* resolver */time_t        keepalive_header;        /* keepalive_timeout */ngx_uint_t    keepalive_requests;      /* keepalive_requests */ngx_uint_t    keepalive_disable;       /* keepalive_disable */ngx_uint_t    satisfy;                 /* satisfy */ngx_uint_t    lingering_close;         /* lingering_close */ngx_uint_t    if_modified_since;       /* if_modified_since */ngx_uint_t    max_ranges;              /* max_ranges */ngx_uint_t    client_body_in_file_only; /* client_body_in_file_only */ngx_flag_t    client_body_in_single_buffer;/* client_body_in_singe_buffer */ngx_flag_t    internal;                /* internal */ngx_flag_t    sendfile;                /* sendfile */ngx_flag_t    aio;                     /* aio */ngx_flag_t    aio_write;               /* aio_write */ngx_flag_t    tcp_nopush;              /* tcp_nopush */ngx_flag_t    tcp_nodelay;             /* tcp_nodelay */ngx_flag_t    reset_timedout_connection; /* reset_timedout_connection */ngx_flag_t    absolute_redirect;       /* absolute_redirect */ngx_flag_t    server_name_in_redirect; /* server_name_in_redirect */ngx_flag_t    port_in_redirect;        /* port_in_redirect */ngx_flag_t    msie_padding;            /* msie_padding */ngx_flag_t    msie_refresh;            /* msie_refresh */ngx_flag_t    log_not_found;           /* log_not_found */ngx_flag_t    log_subrequest;          /* log_subrequest */ngx_flag_t    recursive_error_pages;   /* recursive_error_pages */ngx_uint_t    server_tokens;           /* server_tokens */ngx_flag_t    chunked_transfer_encoding; /* chunked_transfer_encoding */ngx_flag_t    etag;                    /* etag */#if (NGX_HTTP_GZIP)ngx_flag_t    gzip_vary;               /* gzip_vary */ngx_uint_t    gzip_http_version;       /* gzip_http_version */ngx_uint_t    gzip_proxied;            /* gzip_proxied */#if (NGX_PCRE)ngx_array_t  *gzip_disable;            /* gzip_disable */
#endif
#endif#if (NGX_THREADS || NGX_COMPAT)ngx_thread_pool_t         *thread_pool;ngx_http_complex_value_t  *thread_pool_value;
#endif#if (NGX_HAVE_OPENAT)ngx_uint_t    disable_symlinks;        /* disable_symlinks */ngx_http_complex_value_t  *disable_symlinks_from;
#endifngx_array_t  *error_pages;             /* error_page */ngx_path_t   *client_body_temp_path;   /* client_body_temp_path */ngx_open_file_cache_t  *open_file_cache;time_t        open_file_cache_valid;ngx_uint_t    open_file_cache_min_uses;ngx_flag_t    open_file_cache_errors;ngx_flag_t    open_file_cache_events;ngx_log_t    *error_log;ngx_uint_t    types_hash_max_size;ngx_uint_t    types_hash_bucket_size;ngx_queue_t  *locations;#if 0ngx_http_core_loc_conf_t  *prev_location;
#endif
};

可以看出来这个ngx_http_core_loc_conf_s还是比较长的,不过可以看出来这个就是对配置文件 http 里面的解析。

ngx_http_core_srv_conf_t


typedef struct {/* array of the ngx_http_server_name_t, "server_name" directive */ngx_array_t                 server_names;/* server ctx */ngx_http_conf_ctx_t        *ctx;ngx_str_t                   server_name;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                  client_header_timeout;ngx_flag_t                  ignore_invalid_headers;ngx_flag_t                  merge_slashes;ngx_flag_t                  underscores_in_headers;unsigned                    listen:1;
#if (NGX_PCRE)unsigned                    captures:1;
#endifngx_http_core_loc_conf_t  **named_locations;
} ngx_http_core_srv_conf_t;

ngx_http_core_main_conf_t

先看看定义

typedef struct {ngx_array_t                servers;         /* ngx_http_core_srv_conf_t */ngx_http_phase_engine_t    phase_engine;ngx_hash_t                 headers_in_hash;ngx_hash_t                 variables_hash;ngx_array_t                variables;         /* ngx_http_variable_t */ngx_array_t                prefix_variables;  /* ngx_http_variable_t */ngx_uint_t                 ncaptures;ngx_uint_t                 server_names_hash_max_size;ngx_uint_t                 server_names_hash_bucket_size;ngx_uint_t                 variables_hash_max_size;ngx_uint_t                 variables_hash_bucket_size;ngx_hash_keys_arrays_t    *variables_keys;ngx_array_t               *ports;ngx_http_phase_t           phases[NGX_HTTP_LOG_PHASE + 1];
} ngx_http_core_main_conf_t;

流程分析

在/nginx-1.14.2/src/http/ngx_http.c目录下,看一下对于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
};

然后看一下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;//判断if (*(ngx_http_conf_ctx_t **) conf) {return "is duplicate";}/* the main http context *///分配内存 也是当前的上下文ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));if (ctx == NULL) {return NGX_CONF_ERROR;}//进行赋值 利用指针指向地址*(ngx_http_conf_ctx_t **) conf = ctx;/* count the number of the http modules and set up their indices *///计算当前http模块的数值ngx_http_max_module = ngx_count_modules(cf->cycle, NGX_HTTP_MODULE);/* the http main_conf context, it is the same in the all http contexts *///内存池中分配内存 main_confctx->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*///内存池中分配内存 srv_confctx->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*///内存池中分配内存 loc_confctx->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; cf->cycle->modules[m]; m++) {if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {continue;}module = cf->cycle->modules[m]->ctx;mi = cf->cycle->modules[m]->ctx_index;/*接下来进行初始化 类型为 http模块的文件 符合的有ngx_http_core_modulngx_http_log_modulengx_http_upstream_modulengx_http_limit_req_module等等注意不是赋值只是把初始化的配置文件的指针 赋给文件数组 例如 ctx->main_conf*/if (module->create_main_conf) {ctx->main_conf[mi] = module->create_main_conf(cf);if (ctx->main_conf[mi] == NULL) {return NGX_CONF_ERROR;}}/*同上初始化 每个模块的 ctx->srv_conf*/if (module->create_srv_conf) {ctx->srv_conf[mi] = module->create_srv_conf(cf);if (ctx->srv_conf[mi] == NULL) {return NGX_CONF_ERROR;}}/*同上初始化 每个模块的 ctx->loc_conf*/if (module->create_loc_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; cf->cycle->modules[m]; m++) {if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {continue;}/*调用preconfiguration 比如 ngx_http_core_module_ctx 中的 ngx_http_core_preconfiguration方法用途 主要为了初始化cf中的main_conf的variables_keys变量详见当前目录的ngx_http_variables.c中的ngx_http_variables_add_core_vars变量*/module = cf->cycle->modules[m]->ctx;if (module->preconfiguration) {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;/*继续解析剩下的配置server {listen       80;server_name  127.0.0.1;location / {root   html;index  index.html index.htm;}}*///继续解析配置rv = ngx_conf_parse(cf, NULL);if (rv != NGX_CONF_OK) {goto failed;}

这里看到我们这里是到ngx_conf_parse,因为接下来解析剩下来的配置,剩下来的配置说完说ngx_conf_parse剩下的配置.
然后server的配置解析详见接下来的篇章,详见 【nginx读取配置文件http模块之server】

【nginx读取配置文件http模块】相关推荐

  1. python读取配置文件configparser模块

    python使用自带的configparser模块用来读取配置文件,配置文件的形式类似windows中的ini文件 目录 1.新建配置文件 2.读取配置文件 3.打印读取内容 1.新建配置文件 con ...

  2. Nginx 源码分析-- 模块module 解析执行 nginx.conf 配置文件流程分析 一

    搭建nginx服务器时,主要的配置文件 nginx.conf 是部署和维护服务器人员经常要使用到的文件, 里面进行了许多服务器参数的设置.那么nginx 以模块 module为骨架的设计下是如何运用模 ...

  3. nginx启动完访问网页出现HTTP 错误 500.19 - Internal Server Error错误代码0x80070003 无法读取配置文件

    HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息: 模块    IIS Web Core 通知    未知 ...

  4. python读取配置文件获取所有键值对_python读取配置文件 变量 ConfigParser模块

    Python 读取写入配置文件很方便,可使用内置的 configparser 模块 配置文件:config.ini [oppo] platformName = Android platformVers ...

  5. Nginx主配置文件nginx.conf详细说明

    第3章 Nginx核心配置文件nginx.conf史上最细中文详解 3.1定义Nginx运行的用户和用户组 user nginx nginx;#改为特殊的用户和组 3.2 nginxworker进程数 ...

  6. Nginx的配置文件nginx.conf详解

    Nginx的配置文件nginx.conf配置详解如下: user nginx nginx ; Nginx用户及组:用户 组.window下不指定 worker_processes 8; 工作进程:数目 ...

  7. Nginx主配置文件nginx.conf中文详解

    第1章 nginx配置解释图解 第2章 Nginx核心配置文件nginx.conf史上最细中文详解 2.1 定义Nginx运行的用户和用户组 2.2 nginxworker进程数,即处理请求的进程(熟 ...

  8. Nginx 的配置文件介绍

    Nginx配置文件主要分成四部分: 1 2 3 4 5 6 7 8 9 10 11 12 main(全局设置).server(主机设置).upstream(负载均衡服务器设置)和 location(U ...

  9. nginx.conf 配置文件详解

    本文来说下nginx.conf 配置文件详解 文章目录 Nginx 配置文件概述 nginx.conf 配置文件 通用(这里是 windows 系统系 nginx1.14.2 下 nginx.conf ...

最新文章

  1. 关于redis的几件小事(三)redis的数据类型与使用场景
  2. 关于SQL Server 2017中使用json传参时解析遇到的多层解析问题
  3. 【华为云技术分享】从部署和运维说说DLI(1)
  4. 团队管理(1)---故事与哲理(一)
  5. js 斐波那契数列(兔子问题)
  6. dataguard如何实现切换_ORACLE dataguard 切换
  7. Vim编辑器常用命令
  8. arm汇编—bic ,orr
  9. 中台能力是什么?PaaS是什么?微服务是什么?
  10. 怎样使用计算机上的高级共享设置密码,怎么设置电脑共享密码
  11. 《VALORANT》: 双塔迷城的诞生
  12. Java 输出杨辉三角
  13. I2C读取EEROM时序图
  14. SQLMap的基本使用-靶场墨者学院(DB2数据库)
  15. fc安卓模拟器_面对悠长假期,GPD WIN2掌机让我畅玩模拟器游戏
  16. 宁波旭升特斯拉 EDI项目案例
  17. 家庭财务小管家c语言程序,家庭理财小管家下载
  18. GROMACS 分子模拟使用
  19. 浅谈梯度下降与模拟退火算法
  20. nRF51822蓝牙开发

热门文章

  1. 从政府项目中总结出的B端产品账号权限管理
  2. Linux下安装配置使用python虚拟环境
  3. python 经纬度计算相关
  4. C语言找中位数(一位数组)
  5. 防火墙、路由器、交换机+模拟软件+流控
  6. SCP,NFS,TFTP的初步认识
  7. 基于单片机多功能万年历系统毕业设计(完整资料)
  8. 基于Linux下的即时通讯聊天室项目(全代码 有注释 可直接运行)
  9. 源码看JAVA【五】Byte
  10. 香蕉派 BPI-M2 Zero 四核开源单板计算机 全志 H2+/H3 芯片 高端设计