微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

简单nginx module 学习

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

项目结构

├── 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位置的,是一个布尔类型的)

foo_enabled on;

说明

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

参考资料

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐