上一文基本都是从书中学来的。例子也基本都是书中的。接下来尝试着自己写了个超级简单的自定义模块。另外加了参数的解析:
照旧先将demo代码贴上:
#include
#include
#include
static char* ngx_http_mytest2(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void* ngx_http_mytest_create_loc_conf(ngx_conf_t *cf);
static ngx_int_t ngx_http_mytest_handler2(ngx_http_request_t *r);
//自定义的参数结构体
typedef struct {
ngx_str_t my_config_str;
ngx_int_t my_config_num;
} ngx_http_mytest_conf_t;
static ngx_command_t ngx_http_mytest_commands[] = {
{
ngx_string("mytest2"),//指令名称,在配置文件中使用
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE12,
ngx_http_mytest2, // 回调函数
NGX_HTTP_LOC_CONF_OFFSET, //位置
0, //指令的值保存的位置
NULL
},
ngx_null_command
};
static char* ngx_http_mytest2(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
/*//注意,参数HTTP框架传给用户的在ngx_http_mytest_create_loc_conf回调方法中分配的结构体ngx_http_mytest_conf_t*/
ngx_http_mytest_conf_t *mycf = conf;
/* cf->args是1个ngx_array_t队列,它的成员都是ngx_str_t结构。我们用value指向ngx_array_t的elts内容,其中
value[1]就是第1个参数,同理,value[2]是第2个参数
*/
ngx_str_t *value = cf->args->elts;
// ngx_array_t的nelts表示参数的个数
if (cf->args->nelts > 1)
{
// 直接赋值即可,ngx_str_t结构只是指针的传递
mycf->my_config_str = value[1];
}
if (cf->args->nelts > 2)
{
// 将字符串形式的第2个参数转为整型
mycf->my_config_num = ngx_atoi(value[2].data, value[2].len);
/*如果字符串转化整型失败,将报“invalid number”错误,Nginx启动失败*/
if (mycf->my_config_num == NGX_ERROR) {
return "invalid number";
}
}
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_mytest_handler2;
// 返回成功
return NGX_CONF_OK;
}
static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
ngx_http_mytest_create_loc_conf,
NULL
};
ngx_module_t ngx_http_mytest_module2 = {
NGX_MODULE_V1,
&ngx_http_mytest_module_ctx,
ngx_http_mytest_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
//回调函数 handler
static ngx_int_t ngx_http_mytest_handler2(ngx_http_request_t *r)
{
ngx_http_mytest_conf_t *mycf = ngx_http_get_module_loc_conf(r, ngx_http_mytest_module2);
if(!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD)))
return NGX_HTTP_NOT_ALLOWED;
ngx_int_t rc = ngx_http_discard_request_body(r);
if(rc != NGX_OK)
return rc;
ngx_str_t type = ngx_string("text/plain");
char a1[] = "Hello, this is nginx world!";
char a2[mycf->my_config_str.len +1] ;
size_t i=0;
while(i < mycf->my_config_str.len){
a2[i]=mycf->my_config_str.data[i];
i++;
}
a2[i] = '\0';
char a3[sizeof(a1)+sizeof(a2) -1];
memset(a3,0,sizeof(a1)+sizeof(a2) -1);
strcat(a3,a2);
strcat(a3,a1);
ngx_str_t response = ngx_string(a3);
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response.len;
r->headers_out.content_type = type;
rc = ngx_http_send_header(r);
if(rc == NGX_ERROR || rc > NGX_OK || r->header_only)
return rc;
ngx_buf_t *b;
b = ngx_create_temp_buf(r->pool, response.len);
if(b == NULL)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
ngx_memcpy(b->pos, response.data, response.len);
b->last = b->pos + response.len;
b->last_buf = 1;
ngx_chain_t out; //构造输出链表
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
static void* ngx_http_mytest_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_mytest_conf_t *mycf;
mycf = (ngx_http_mytest_conf_t *)ngx_pcalloc(cf->pool, sizeof(ngx_http_mytest_conf_t));
if (mycf == NULL) {
return NULL;
}
//mycf->my_config_str = NGX_CONF_UNSET;
mycf->my_config_num = NGX_CONF_UNSET;
return mycf;
}
其实跟之前的例子就是多了一个队参数的解析。定义了一个结构体。然后 在ctx 的 create_loc_conf 方法来创建保存参数的结构体。在ngx_command_t 的set方法里面可以获取到哪个参数。然后来处理。这个例子很简单。