Nginx的http开发中用两种介入方式打印“hello world”

本文介绍了在Nginx的HTTP开发中,通过设置ngx_core_loc_conf_t结构体的handler指针和配置项回调函数实现打印'hello world'的两种方式。在源代码中创建新的模块文件,并在nginx.conf的server模块下配置location来达到目的。
摘要由CSDN通过智能技术生成
在对nginx进行http开发时,有两种方式介入http模块,具体可以参考 nginx模块定制开发中介入http模块的方法及NGX_HTTP_CONTENT_PHASE阶段的详细介绍,下面用代码来具体实现这两种方法打印“hello world”。


方法一、在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,           
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值