ngx_http_sample_module.c文件:
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
ngx_flag_t enable;
ngx_uint_t num;
ngx_str_t key;
} ngx_http_sample_loc_conf_t;
static ngx_int_t ngx_http_sample_handler(ngx_http_request_t *r);
static void *ngx_http_sample_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_sample_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_sample_init(ngx_conf_t *cf);
static ngx_command_t ngx_http_sample_commands[] = {
{ ngx_string("sample"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_sample_loc_conf_t, enable),
NULL },
{ ngx_string("sample_num"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_sample_loc_conf_t, num),
NULL },
{ ngx_string("sample_access_key"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_sample_loc_conf_t, key),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_sample_module_ctx = {
NULL, /* preconfiguration */
ngx_http_sample_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_sample_create_loc_conf, /* create location configuration */
ngx_http_sample_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_sample_module = {
NGX_MODULE_V1,
&ngx_http_sample_module_ctx, /* module context */
ngx_http_sample_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
};
static void *
ngx_http_sample_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_sample_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sample_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET;
conf->num = NGX_CONF_UNSET;
return conf;
}
static char *
ngx_http_sample_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_sample_loc_conf_t *prev = parent;
ngx_http_sample_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->enable, prev->enable, 0);
ngx_conf_merge_uint_value(conf->num, prev->num, 10);
ngx_conf_merge_str_value(conf->key, prev->key, "key");
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_sample_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_sample_handler;
return NGX_OK;
}
static ngx_int_t
ngx_http_sample_handler(ngx_http_request_t *r)
{
ngx_http_sample_loc_conf_t *alcf;
alcf = ngx_http_get_module_loc_conf(r, ngx_http_sample_module);
if (!alcf->enable) {
return NGX_OK;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "access sample module limit:%d", alcf->num);
return NGX_OK;
}
nginx.conf文件http
http {
sample on;
sample 50;
sample_access_key "Symfoware";
}
输入命令“curl http://127.0.0.1/?asdfadsf”进行测试
结果如下:
2016/09/09 16:19:04 [错误] 5063#0:* 1访问样本模块限制:50, 客户端:127.0.0.1,服务器:localhost,请求:“GET /?asdfadsf HTTP / 1.1“,主机:”127.0.0.1“
2016/09/09 16:19:04 [错误] 5063#0:* 1访问示例模块限制:50,客户端:127.0.0.1,服务器:localhost,请求:“GET /?asdfadsf HTTP / 1.1“,host:”127.0.0.1“
问题如下:
当我发送请求时,代码会处理请求两次。
我想知道如何处理这个问题。 感谢。