#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_DEBUG, r->connection->log, 0, "access sample module limit:%d", alcf->num);
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "access sample module key:%V", &alcf->key);
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,请求:&#34; GET /?asdfadsf HTTP / 1.1& #34;,主持人:&#34; 127.0.0.1&#34;
2016/09/09 16:19:04 [error] 5063#0: *1 access sample module key:Symfoware, client: 127.0.0.1, server: localhost, request: "GET /?asdfadsf HTTP/1.1", host: "127.0.0.1"
2016/09/09 16:19:04 [error] 5063#0: *1 access sample module limit:50, client: 127.0.0.1, server: localhost, request: "GET /?asdfadsf HTTP/1.1", host: "127.0.0.1"
2016/09/09 16:19:04 [error] 5063#0: *1 access sample module key:Symfoware, client: 127.0.0.1, server: localhost, request: "GET /?asdfadsf HTTP/1.1", host: "127.0.0.1"
问题是一次GET请求,而通过nginx module输出的却是两次请求的处理。