问题: 自己实现的ngnix 模块无法正常解析enum参数
环境: openresty 1.11.2, Centos 7.3
模块文件如下:
$ ll
total 20
-rw-rw-r--. 1 samuel samuel 157 Jan 18 09:49 config
-rw-rw-r--. 1 samuel samuel 1566 Jan 25 11:43 ngx_http_test_module.c
模块代码如下:
$ cat config
ngx_addon_name=ngx_http_test_module
HTTP_MODULES="$HTTP_MODULES ngx_http_test_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_test_module.c"
$ cat ngx_http_test_module.c
// 《深入理解Ngnix》, 第4章例子
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
ngx_uint_t my_enum_seq;
} ngx_http_test_conf_t;
static void *ngx_http_test_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_test_conf_t *mycf;
mycf = (ngx_http_test_conf_t *) ngx_pcalloc(cf->pool, sizeof(ngx_http_test_conf_t));
if (mycf == NULL) {
return NULL;
}
return mycf;
}
static ngx_conf_enum_t test_enums[] = {
{ngx_string("apple"), 1},
{ngx_string("banana"), 2},
{ngx_string("orange"), 3},
{ngx_null_string, 0},
};
// ?
static char *ngx_http_hello_set_enum(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_test_conf_t *local_conf;
local_conf = conf;
char *rv = NULL;
rv = ngx_conf_set_enum_slot(cf, cmd, conf);
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "test_enum: %ui", local_conf->my_enum_seq);
return rv;
}
static ngx_command_t test_commands[] = {
{
ngx_string("test_enum"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_http_hello_set_enum,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_test_conf_t, my_enum_seq),
test_enums,
},
ngx_null_command
};
static ngx_http_module_t test_ctx = {
NULL, NULL, NULL, NULL, NULL, NULL, ngx_http_test_create_loc_conf, NULL
};
ngx_module_t ngx_http_test_module = {
NGX_MODULE_V1,
&test_ctx,
test_commands,
NGX_HTTP_MODULE,
NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NGX_MODULE_V1_PADDING
};
配置文件:
$ cat conf/nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location /xxxx {
test_enum apple;
}
}
}
启动ngnix, 输入日志如下
nginx: [emerg] test_enum: 0 in /home/samuel/openresty/nginx/conf/nginx.conf:21
nginx: [emerg] "test_enum" directive is duplicate in /home/samuel/openresty/nginx/conf/nginx.conf:21