zoukankan      html  css  js  c++  java
  • Nginx模块开发-理解HTTP配置

    理解HTTP配置

    相关数据结构

    先明白Nginx下述数据结构,再理解 HTTP配置的解析与合并过程

    typedef struct{
        NGX_MODULE_V1;
        void *ctx;
        ngx_command_t *commands;
        ngx_uint_t type;
        ngx_int_t (*init_master)(ngx_log_t *log);
        ngx_int_t (*init_module)(ngx_cycle_t *cycle);
        ngx_int_t (*init_process)(ngx_cycle_t *cycle);
        ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
        void (*exit_thread)(ngx_cycle_t *cycle);
        void (*exit_process)(ngx_cycle_t *cycle);
        void (*exit_master)(ngx_cycle_t *cycle);
        NGX_MODULE_V1_PADDING;
    } ngx_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;
    
    typedef struct{
        ngx_str_t name;
        ngx_uint_t type;
        char *(set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
        ngx_uint_t conf;
        ngx_uint_t offset;
        void *post;
    } ngx_command_t;
    

    HTTP配置的内存布局

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

    HTTP配置的解析

    主循环=>配置文件解析器=>HTTP框架(ngx_http_module, ngx_http_core_module)

    • 主循环调用配置文件解析器解析nginx.conf
    • 解析器调用HTTP框架(主要是ngx_http_module, ngx_http_core_module)解析http {}。
    • HTTP框架遍历http {}, 创建ngx_http_conf_ctx_t变量,调用modules.ctx.create_main_conf()/create_srv_conf()/create_loc_conf()分配内存,调用modules.ctx.preconfiguration()预先初始化,调用modules.commands.set()解析配置项。
    • HTTP框架遍历http {}里的server {}, 创建ngx_http_conf_ctx_t变量, 调用modules.ctx.create_srv_conf()/create_loc_conf()分配内存,调用modules.commands.set()解析配置项。
    • HTTP框架遍历server {}里的location {}, 创建ngx_http_conf_ctx_t变量,调用modules.ctx.create_loc_conf()分配内存,调用modules.commands.set()解析配置项。

    HTTP配置的合并

    • HTTP配置的解析过程为每个http {}, server {}, location {}都创建ngx_http_conf_ctx_t变量, main_conf, srv_conf, loc_conf保存modules.create_main_conf()/create_srv_conf()/create_loc_conf()的结果。同时根据http {}, server {}, location {}的层级关系组织为树状。
    • HTTP配置的合并过程实际是分别遍历server {}, location {}层的ngx_http_conf_ctx_t变量及其父级变量,调用modules.ctx.merge_srv_conf()/merge_loc_conf()合并上一级的值。
      其中,location {}层调用merge_loc_conf()即可。

    理解关键点

    • HTTP框架为每个http {}, server {}, location {}都生成一个ngx_http_conf_ctx_t变量。这些变量根据http {}, server {}, location{}的树状关联起来。
    • HTTP框架调用modules.ctx.create_main_conf()/create_srv_conf()/create_loc_conf()分配内存。对于http {}还要调用moudles.ctx.preconfiguration()执行预初始化。
    • HTTP框架调用modules.commands.set()解析配置项。
    • HTTP框架根据树状执行merge操作。就是遍历调用modules.ctx.merge_srv_conf()/merge_loc_conf()操作。
  • 相关阅读:
    (Java实现) 洛谷 P1603 斯诺登的密码
    (Java实现) 洛谷 P1036 选数
    (Java实现) 洛谷 P1036 选数
    (Java实现) 洛谷 P1012 拼数
    (Java实现) 洛谷 P1012 拼数
    (Java实现) 洛谷 P1028 数的计算
    (Java实现) 洛谷 P1028 数的计算
    (Java实现) 洛谷 P1553 数字反转(升级版)
    8.4 确定两个日期之间的月份数或年数
    (Java实现) 洛谷 P1553 数字反转(升级版)
  • 原文地址:https://www.cnblogs.com/zolo/p/5846225.html
Copyright © 2011-2022 走看看