也来学习写一下nginx的自定义模块

本文介绍了如何使用nginx自定义模块实现特定URL的定制响应,包括建立目录、配置文件、模块代码编写、编译安装及配置测试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这几天在家里,学习了一下nginx的一些东西,首先当然就是用编译安装来配置简单的服务了。上一篇也算是学习配置的一点心得,在网上看了nginx的可扩展性后,我也简单研究了一下nginx的自定义模块。并且在晚上代码的参考下,也编译了两个模块来玩了一下,这里先展示一下一个最简单的模块。

这个模块主要是这样一个目的,在输入一个指定的url后,会返回自定义的内容:

url:http://127.0.0.1/hello

输出内容为我自定义的字符串,比如:”I try to fuck Nginx!!!”

这里介绍以下步骤:

第一步:

建立任意一个目录,比如:ngx_hello

第二步:

在这个文件夹中建立一个config文件,内容如下:

ngx_addon_name=ngx_xtest  #这里指定模块名称
HTTP_MODULES=”$HTTP_MODULES ngx_xtest” #指定编译后文件名称
NGX_ADDON_SRCS=”$NGX_ADDON_SRCS $ngx_addon_dir/ngx_xtest.c”  #指定源文件,这里我只有一个文件多个文件就要全部写上去

CORE_LIBS=”$CORE_LIBS ” #这里指定编译的nginx库,这变量是由编译nginx的时候Makefile传递过来的,上面的几个变量也是一样

第三步:

建立ngx_xtest.c文件,内容如下,也写了简单的注释
//引用nginx头文件,我们这个测试文件就一个没有其它的测试文件,所以也没有引用其它的头文件。
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

char* ngx_xtest_setup(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

u_char ngx_xtest_string[] = “I try to FUCK Nginx !!!”;
//这个事命令组,这里我们可以写多个命令,以数组的形式存在
static ngx_command_t ngx_xtest_commands[] =
{
{   ngx_string(“hello”),//这是命令字段,其实也就是我们在url中访问时写的目标文件
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_xtest_setup,      //这个函数来响应hello这个命令,hello命令触发这个函数
0,
0,
NULL
},
ngx_null_command
};//这个数组必须以ngx_null_command结尾

//这个是用来处理配置文件的,这里我们使用系统的默认配置文件处理
static ngx_http_module_t ngx_xtest_ctx = {
NULL,                          /* preconfiguration */
NULL,                          /* postconfiguration */
NULL,                          /* create main configuration */
NULL,                          /* init main configuration */
NULL,                          /* create server configuration */
NULL,                          /* merge server configuration */
NULL,                          /* create location configuration */
NULL                           /* merge location configuration */
};

//这个才是真正的模块。
ngx_module_t ngx_xtest = {
NGX_MODULE_V1,
&ngx_xtest_ctx,       /* module context */
ngx_xtest_commands,   /* module directives */
NGX_HTTP_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
};//实例化模块对象

//hello命令的真正处理函数,参数是ngx_http_request_t即一个请求
//此函数实现的也是基本处理流程
static ngx_int_t ngx_xtest_handler(ngx_http_request_t *r){
ngx_int_t    rc;
ngx_buf_t   *b;
ngx_chain_t  out;

//填充HTTP头
/* set the ‘Content-type’ header */
r->headers_out.content_type.len = sizeof(“text/html”) – 1;
r->headers_out.content_type.data = (u_char *) “text/html”;

//分配输出内存空间
/* allocate a buffer */
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
//输出缓存附加到输出链表上
/* attach buffer to the buffer chain */
out.buf = b;
out.next = NULL;

//填写输出缓存内容
/* adjust the pointers of the buffer */
b->pos = ngx_xtest_string;  /* the begin offset of the buffer */
b->last = ngx_xtest_string + sizeof(ngx_xtest_string) – 1; /* the end offset of the buffer */
b->memory = 1;    /* this buffer is in memory */
b->last_buf = 1;  /* this is the last buffer in the buffer chain */

/* 设置http返回码 */
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = sizeof(ngx_xtest_string) – 1;

//发送HTTP报头
/* send the headers of your response */
rc = ngx_http_send_header(r);

if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
//输出内容
/* send the buffer chain of your response */
return ngx_http_output_filter(r, &out);
}

//实现hello命令的初始化函数,此函数指定命令的真正处理函数为ngx_xtest_handler
char* ngx_xtest_setup(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;

clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_xtest_handler; /* handler to process the ‘hello’ directive */

return NGX_CONF_OK;
}

第四步:编译安转

在编译时需要加入–add-module=/path

$./configure –prefix=/usr/local/nginx –add-module=/data/babycode/nginx/ngx_xmodule/ –with-debug

$make

#make install

第五步:修改配置
配置文件—nginx.conf

location /hello{

hello;

}
第六步:运行测试
#/usr/local/nginx/sbin/nginx
测试

在浏览器中输入:http://127.0.0.1/hello

浏览器中会出现”I try to FUCK Nginx !!!”,则表示运行成功拉。。。

OK! 今天就先学习到这里,打完收工,睡觉拉。。。

### 编Nginx自定义模块中的Access模块Config文件 为了正确编Nginx自定义模块(特别是`access`模块)的配置文件,需要遵循一定的结构和逻辑。以下是详细的说明: #### 配置文件的作用 在Nginx中,`config`文件用于指定模块的构建选项以及依赖关系。对于`access`模块来说,该文件主要用于描述模块的名称、类型、源码路径以及其他必要的参数。 --- #### Config 文件模板 以下是一个典型的 `config` 文件示例,适用于 Nginx 自定义模块(如 `access` 模块)[^4]: ```bash ngx_addon_name=ngx_http_access_custom_module if test -n "$ngx_module_link"; then ngx_module_type=HTTP_ACCESS ngx_module_name=$ngx_addon_name ngx_module_incs= ngx_module_deps= ngx_module_srcs="$ngx_addon_dir/ngx_http_access_custom_module.c" ngx_module_libs= . auto/module else HTTP_MODULES="$HTTP_MODULES ngx_http_access_custom_module" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_access_custom_module.c" fi ``` --- #### 参数解释 1. **`ngx_addon_name`**: 定义模块的名字,在此例子中命名为 `ngx_http_access_custom_module`。 2. **`ngx_module_type`**: 设置模块的类型为 `HTTP_ACCESS`,表示这是一个访问控制类型的模块[^3]。 3. **`ngx_module_srcs`**: 列出模块的主要 C 源代码文件位置,这里指定了 `ngx_http_access_custom_module.c`。 4. **条件判断 (`if-then-else`)**: 如果设置了 `$ngx_module_link` 变量,则通过 `. auto/module` 脚本动态链接模块;否则静态编译模块并将其加入全局变量 `HTTP_MODULES` 和 `NGX_ADDON_SRCS` 中。 --- #### Access 模块的工作机制 `access` 类型的模块主要负责处理客户端请求权限验证的任务。它会在请求进入内容处理器之前运行,并决定是否允许继续执行后续阶段。如果在此阶段调用了 `ngx.exit()` 函数,则会立即终止当前请求[^5]。 --- #### 实际应用案例 假设我们正在开发一个简单的 `access` 模块来限制特定 IP 地址范围内的用户访问资源。那么对应的 `config` 文件可能如下所示: ```bash ngx_addon_name=ngx_http_ip_restrict_module if test -n "$ngx_module_link"; then ngx_module_type=HTTP_ACCESS ngx_module_name=$ngx_addon_name ngx_module_incs= ngx_module_deps= ngx_module_srcs="$ngx_addon_dir/ngx_http_ip_restrict_module.c" ngx_module_libs= . auto/module else HTTP_MODULES="$HTTP_MODULES ngx_http_ip_restrict_module" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_ip_restrict_module.c" fi ``` 这里的模块名为 `ngx_http_ip_restrict_module`,并且实现了基于 IP 的访问控制功能。 --- #### 构建过程注意事项 1. 将上述 `config` 文件放置于模块根目录下。 2. 使用 `./configure --add-module=/path/to/custom_module` 命令将模块添加到 Nginx 的构建过程中[^2]。 3. 执行 `make && make install` 来完成模块的编译与安装。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值