在对nginx进行http开发时,有两种方式介入http模块,具体可以参考
nginx模块定制开发中介入http模块的方法及NGX_HTTP_CONTENT_PHASE阶段的详细介绍,下面用代码来具体实现这两种方法打印“hello world”。
方法一、在ngx_http_module_t接口的postconfiguration方法中将自定义的方法添加到handler动态数组中
源文件代码如下:
方法一、在ngx_http_module_t接口的postconfiguration方法中将自定义的方法添加到handler动态数组中
源文件代码如下:
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t ngx_http_hello_world_init(ngx_conf_t *cf);
static ngx_http_module_t ngx_http_hello_world_module_ctx = {
NULL, /* preconfiguration */
ngx_http_hello_world_init, /* postconfiguration */ //该步骤必定会被调用,所以在此介入
NULL, /* create main configuration. */
NULL, /* init main configuration. */
NULL, /* create server configuration. */
NULL, /* merge server configuration. */
NULL,
NULL /* merge location configuration. */
};
ngx_module_t ngx_http_hello_world_module = {
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx, /*module context*/
NULL, /*module directives*/
NGX_HTTP_MODULE, /*module type*/
NULL, /*init master*/
NULL, /*init module*/
NULL, /*init process*/
NULL, /*init thread*/
NULL,