• 简单nginx module 学习


    来自官方文档,主要是学习下构建以及集成

    项目结构

    • 一个nginx 模块至少包含一个config 以及模块代码
    ├── config
    └── ngx_foo_module.c
    • 代码
      config 关于模块类型、模块名称以及依赖的代码定义,当然也可以包含feature(比如模块依赖的库文件检查等)
     
    ngx_module_type=CORE
    ngx_module_name=ngx_foo_module
    ngx_module_srcs="$ngx_addon_dir/ngx_foo_module.c"
    . auto/module
    ngx_addon_name=$ngx_module_name

    ngx_foo_module.c 代码(模块功能,一般包含配置,指令,以及指令解析处理,以及模块的执行阶段)

    /*
     * Copyright (C) Author.
     */
     
     
    #include <ngx_config.h>
    #include <ngx_core.h>
     
     
    typedef struct {
        ngx_flag_t  enable;
    } ngx_foo_conf_t;
     
     
    static void *ngx_foo_create_conf(ngx_cycle_t *cycle);
    static char *ngx_foo_init_conf(ngx_cycle_t *cycle, void *conf);
     
    static char *ngx_foo_enable(ngx_conf_t *cf, void *post, void *data);
    static ngx_conf_post_t  ngx_foo_enable_post = { ngx_foo_enable };
     
     
    static ngx_command_t  ngx_foo_commands[] = {
     
        { ngx_string("foo_enabled"),
          NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
          ngx_conf_set_flag_slot,
          0,
          offsetof(ngx_foo_conf_t, enable),
          &ngx_foo_enable_post },
     
          ngx_null_command
    };
     
     
    static ngx_core_module_t  ngx_foo_module_ctx = {
        ngx_string("foo"),
        ngx_foo_create_conf,
        ngx_foo_init_conf
    };
     
     
    ngx_module_t  ngx_foo_module = {
        NGX_MODULE_V1,
        &ngx_foo_module_ctx,                   /* module context */
        ngx_foo_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 void *
    ngx_foo_create_conf(ngx_cycle_t *cycle)
    {
        ngx_foo_conf_t  *fcf;
     
        fcf = ngx_pcalloc(cycle->pool, sizeof(ngx_foo_conf_t));
        if (fcf == NULL) {
            return NULL;
        }
     
        fcf->enable = NGX_CONF_UNSET;
     
        return fcf;
    }
     
     
    static char *
    ngx_foo_init_conf(ngx_cycle_t *cycle, void *conf)
    {
        ngx_foo_conf_t *fcf = conf;
     
        ngx_conf_init_value(fcf->enable, 0);
     
        return NGX_CONF_OK;
    }
     
     
    static char *
    ngx_foo_enable(ngx_conf_t *cf, void *post, void *data)
    {
        ngx_flag_t  *fp = data;
     
        if (*fp == 0) {
            return NGX_CONF_OK;
        }
     
        ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "Foo Module is enabled");
     
        return NGX_CONF_OK;
    }
     
     

    构建

    基于了mac 构建,需要自己添加一些依赖

    ./configure  --with-openssl=/usr/local/opt/openssl@3 --with-debug --prefix=$PWD --with-cc-opt='-O0 -g'  --add-module=../../modules/mymodules/
    make

    效果

    使用

    上边的模块包含指令的位置(属于main位置的,是一个布尔类型的)

    • nginx.conf 配置
    foo_enabled on;
    • 效果

    说明

    以上是一个简单的学习,官方文档是比较推荐学习的

    参考资料

    https://nginx.org/en/docs/dev/development_guide.html#Modules
    https://github.com/baishancloud/nginx-development-guide/blob/master/zh.md

  • 相关阅读:
    PHP之目录遍历
    PHP之验证码
    PHP之验证码
    PHP之异常处理模式
    PHP之pdo的预处理模式
    PHP之PDO
    PHP之cookie和session
    PHP之MVC
    单例模式
    ThreadLocal
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/16104137.html
Copyright © 2020-2023  润新知