/* * Copyright (C) FatHong */ #include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> /* 5、配置结构体,这个专属于 ngx_http_hello_module */ typedef struct { ngx_str_t test; /* test选项的值将存储在这里 */ } ngx_http_hello_loc_conf_t; static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf); static char *ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); /* 4、指令集 */ static ngx_command_t ngx_http_hello_commands[] = { { ngx_string("hello"), /* hello指令,对应配置选项 hello; */ NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_hello, 0, 0, NULL }, { ngx_string("test"), /* test指令,对应配置选项 test "hello world"; */ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_str_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_hello_loc_conf_t, test), /* test选项的值将存储在这里 */ NULL }, ngx_null_command }; /* 2、模块上下文,这里是http类型模块 */ static ngx_http_module_t ngx_http_hello_module_ctx = { NULL, /* preconfiguration */ NULL, /* postconfiguration */ NULL, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_hello_create_loc_conf, /* create location configuration:3.1、配置结构体钩子注册 */ NULL /* merge location configuration */ }; /* 1、模块创建 */ ngx_module_t ngx_http_hello_module = { NGX_MODULE_V1, &ngx_http_hello_module_ctx, /* module context:模块上下文 */ ngx_http_hello_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 u_char ngx_hello_string[] = "hello world"; /* 处理函数 */ static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r) { ngx_int_t rc; ngx_buf_t *b; ngx_chain_t out; ngx_http_hello_loc_conf_t *hrcf; /* 设置响应内容格式,这里为text/html */ r->headers_out.content_type_len = sizeof("text/html") - 1; r->headers_out.content_type.len = sizeof("text/html") - 1; r->headers_out.content_type.data = (u_char *) "text/html"; b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } out.buf = b; out.next = NULL; b->pos = ngx_hello_string; b->last = ngx_hello_string + sizeof(ngx_hello_string) - 1; b->memory = 1; b->last_buf = 1; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = sizeof(ngx_hello_string) - 1; rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } /* 演示如何获取配置选项的值 */ hrcf = ngx_http_get_module_loc_conf(r, ngx_http_hello_module); printf("test: %s\n", hrcf->test.data); // 直接输出到标准输出 return ngx_http_output_filter(r, &out); } /* 3.2 配置结构体就是这样产生的 */ static void * ngx_http_hello_create_loc_conf(ngx_conf_t *cf) { ngx_http_hello_loc_conf_t *hlcf; hlcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_loc_conf_t)); if (hlcf == NULL) { return NULL; } return hlcf; } static char * ngx_http_hello(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_hello_handler; /* 相当于一个钩子 */ return NGX_CONF_OK; }
并且守护进程输出 test: hello world
本文结束,阅读原文:http://nglua.com/reads/2.html