ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

简单nginx module 学习

2022-04-05 22:03:52  阅读:190  来源: 互联网

标签:NGX module 学习 nginx static conf foo ngx


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

项目结构

  • 一个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

标签:NGX,module,学习,nginx,static,conf,foo,ngx
来源: https://www.cnblogs.com/rongfengliang/p/16104137.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有