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"
接著在 C 檔裡定義模組:test_flag、test_str、test_num 用 Nginx 提供的預設解析函式處理,自訂的 mytest 則自己寫 handler。
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
ngx_str_t my_str;
ngx_int_t my_num;
ngx_flag_t my_flag;
ngx_str_t mytest_str;
ngx_int_t mytest_num;
} ngx_http_mytest_conf_t;
static char* ngx_conf_set_mytest(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r);
static void* ngx_http_mytest_create_loc_conf(ngx_conf_t* cf);
static char* ngx_http_mytest_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child);
static ngx_command_t ngx_http_mytest_commands[] = {
{ ngx_string("test_flag"),
NGX_HTTP_LOC_CONF | NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_flag), NULL },
{ ngx_string("test_str"),
NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_str), NULL },
{ ngx_string("test_num"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mytest_conf_t, my_num), NULL },
{ ngx_string("mytest"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
ngx_conf_set_mytest,
NGX_HTTP_LOC_CONF_OFFSET, 0, NULL },
ngx_null_command
};
前三者靠 offsetof 搭配預設 slot 函式自動填進結構體;mytest 因為要同時吃兩個參數並順便掛上 handler,得自己解析。
static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL, NULL, NULL, NULL, NULL, NULL,
ngx_http_mytest_create_loc_conf,
ngx_http_mytest_merge_loc_conf
};
ngx_module_t ngx_http_mytest_module = {
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
};
static char* ngx_conf_set_mytest(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
{
ngx_http_mytest_conf_t* mycf = conf;
ngx_str_t* value = cf->args->elts;
mycf->mytest_num = ngx_atoi(value[2].data, value[2].len);
mycf->mytest_str = value[1];
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_handler;
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r)
{
if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD | NGX_HTTP_POST)))
return NGX_HTTP_NOT_ALLOWED;
ngx_int_t rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) return rc;
ngx_http_mytest_conf_t* my_conf =
ngx_http_get_module_loc_conf(r, ngx_http_mytest_module);
ngx_str_t type = ngx_string("text/plain");
u_char buf[1024] = {0};
ngx_sprintf(buf, "test_str=%s,test_flag=%i,test_num=%i,mytest_str=%s,mytest_num=%i",
my_conf->my_str.data, my_conf->my_flag, my_conf->my_num,
my_conf->mytest_str.data, my_conf->mytest_num);
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = ngx_strlen(buf);
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 = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) return NGX_HTTP_INTERNAL_SERVER_ERROR;
b->pos = buf;
b->last = buf + ngx_strlen(buf);
b->memory = 1;
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* conf =
ngx_pcalloc(cf->pool, sizeof(ngx_http_mytest_conf_t));
if (NULL == conf) return NGX_CONF_ERROR;
conf->my_str.len = 0;
conf->my_str.data = NULL;
conf->my_num = NGX_CONF_UNSET;
conf->my_flag = NGX_CONF_UNSET;
return conf;
}
static char* ngx_http_mytest_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child)
{
ngx_http_mytest_conf_t* prev = parent;
ngx_http_mytest_conf_t* conf = child;
ngx_conf_merge_str_value(conf->my_str, prev->my_str, "");
return NGX_CONF_OK;
}
create 階段把欄位設為 NGX_CONF_UNSET,merge 階段才有辦法判斷「有沒有被設定過」並繼承上層值,這是 Nginx 模組的固定套路。
location /test {
test_flag on;
test_str "helloworld";
test_num 88;
mytest "hello" 88;
}
./configure --add-module=/home/chen123/nginx/exp3 make && make install sudo /usr/local/nginx/sbin/nginx
請求 /test,就會看到五個配置項的值被原封不動印回來。