nginx handler ngx_http_core_module

来源:互联网 发布:手机淘宝秒杀怎么刷新 编辑:程序博客网 时间:2024/06/07 06:10

Nginx 按location配置的加载做法。

ngx_http_test_module.c

/***/#include <ngx_core.h>#include <ngx_http.h>#include <ngx_config.h>static ngx_str_t data = ngx_string("nginx.\n");static char *ngx_http_test_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static ngx_int_t ngx_http_test_handler(ngx_http_request_t *r);static ngx_command_t ngx_http_test_commands[] = {    {    ngx_string("test"),    NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,    ngx_http_test_init,    NGX_HTTP_LOC_CONF_OFFSET,    0,    NULL},    ngx_null_command};static ngx_http_module_t ngx_http_test_ctx = {    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};ngx_module_t ngx_http_test_module = {    NGX_MODULE_V1,&ngx_http_test_ctx,ngx_http_test_commands,NGX_HTTP_MODULE,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 char *ngx_http_test_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {    ngx_http_core_loc_conf_t *clcf;clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);clcf->handler = ngx_http_test_handler;/* *ngx_conf_set_str_slot(cf,cmd,conf); */return NGX_CONF_OK;}static ngx_int_tngx_http_test_handler(ngx_http_request_t *r) {    ngx_buf_t  *b;    ngx_int_t   rc;    ngx_chain_t out;if (r->method & NGX_HTTP_POST) {    return NGX_HTTP_NOT_ALLOWED;}rc = ngx_http_discard_request_body(r);if (NGX_OK != rc) {    return rc;}r->headers_out.status = NGX_HTTP_OK;r->headers_out.content_length_n = data.len;r->headers_out.content_type.data = (u_char *) "text/html";r->headers_out.content_type.len = sizeof("text/html") - 1;if (r->method & NGX_HTTP_HEAD) {    rc = ngx_http_send_header(r);if (NGX_OK != rc) {    return rc;}}b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));if (NULL == b) {    return NGX_HTTP_INTERNAL_SERVER_ERROR;}b->pos = data.data;b->last = data.data + data.len;b->last_buf = 1;b->memory = 1;out.buf = b;out.next = NULL;rc = ngx_http_send_header(r);if (NGX_OK != rc) {    return rc;}return ngx_http_output_filter(r, &out);}

conf

ngx_addon_name=ngx_http_test_moduleHTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_test_module"NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_test_module.c"

指令里用的是NGX_CONF_NOARGS,所以location为

location /test/ {test;}


0 0
原创粉丝点击