本文档为个人博客文档系统的备份版本、作者:小游、作者博客:点击访问
参考https://blog.csdn.net/xiajun07061225/article/details/9130237
首先我们先编写模块,编写模块的时候我们需要新建一个config
文件这个文件就是nginx模块的配置文件
比如我们写一个简单的mytest模块,那么文件的配置信息就是下面这样的。这里我们需要定义三个变量
config文件其实是一个可执行的Shell脚本,如果只想开发一个HTTP模块,需要定义三个变量:
(1)ngx_adon_name。仅在configure执行时使用,一般设置为模块名称。
(2)HTTP_MODULES。保存所有的HTTP模块名称。每个模块间由空格相连。在重新设置这个变量时,不要直接覆盖,因此要如下设置:"$HTTP_MODULES ngx_http_mytest_module"
(3)NGX_ADDON_SRCS。用于指定新模块的源代码,多个待编译的源代码之间可以用空格相连。
注意,在设置这个变量时可以使用$ngx_addon_dir变量(这个变量是用于我们编译的时候传递的),它等价于configure执行时–add-module=PATH的PATH参数。
ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
然后我们新建一个ngx_http_mytest_module.c
文件用于自己的代码,代码内容如下
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
static char *
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
//处理配置项
static ngx_command_t ngx_http_mytest_commands[] = {