Nginx module开发 简单实例——hello world

以最简单的hello world为例。开发一个handler模块完成

参考http://tengine.taobao.org/book/chapter_03.html

 

handler模块开发需要完成3步

  • 编写模块基本结构。包括模块的定义,模块上下文结构,模块的配置结构等。
  • 实现handler的挂载函数。根据模块的需求选择正确的挂载方式。
  • 编写handler处理函数。模块的功能主要通过这个函数来完成。

 

一、模块基本结构

模块配置信息的定义。通常的命名习惯为ngx_http_<module name>_(main|srv|loc)_conf_t,分别是main,server和location。绝大多数模块仅需要一个location配置

hello模块定义如下

 

 

一个hello_string是输入输出的字符串,一个hello_counter是用于计数(访问该网页的次数)

 

模块的配置指令。通常为ngx_command_t类型的数组,具体包含字段和可选参数可参照上面的网站,这里直接给出hello module的配置指令

 

 

 

模块上下文结构。用来创建和合并三个部分的配置(main,server,location),它是一个静态的ngx_http_module_t结构体,命名方式一般是:ngx_http__module_ctx 。具体包含字段和可选参数可参照上面的网站,这里直接给出hello module的上下文结构

 

 

 

模块定义。任何模块,我们都需要定义一个ngx_module_t 类型的变量,来说明这个模块本身的信息,这是这个模块最重要的一个信息,它告诉了nginx这个模块的一些信息,上面定义的配置信息,还有模块上下文信息。加载模块的上层代码,都需要通过定义的这个结构,来获取这些信息。

 

 

 

二、Handler模块处理函数

 

在开发Handler模块时,除了提供上一节介绍的的基本结构体外,handler还需要提供一个处理函数。这个函数负责对来自客户端请求的真正处理。这个函数的处理,既可以选择自己直接生成内容,也可以选择拒绝处理,由后续的handler去进行处理,或者是选择丢给后续的filter进行处理。来看一下这个函数的原型声明:

typedef ngx_int_t (*ngx_http_handler_pt) (ngx_http_request_t * r);

该函数处理成功返回NGX_OK,处理发生错误返回NGX_ERROR,拒绝处理(留给后续的handler进行处理)返回NGX_DECLINE。 返回NGX_OK也就代表给客户端的响应已经生成好了,否则返回NGX_ERROR就发生错误了。

 

Handler一般做4件事:获取location配置、生成合适的响应、发送响应头、发送响应体。Handler有一个参数,即请求结构体。请求结构体包含很多关于客户请求的有用信息,比如说请求方法,URI,请求头等等。

 

 

 

 

 

 

 

三、handler挂载函数

当一切该定义的结构都已经定义好之后,你需要指定所编写的模块挂载的具体方式及位置。详参上面的网站。

 

 

四、编译和运行

 

进入nginx安装目录

cd /home /zbr/src/nginx-1.14.2

 

Ngx_http_hello_module里面的配置文件config

ngx_addon_name=ngx_http_hello_module

HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c"

 

./configure --prefix=/usr/local/nginx --add-module=/home/zbr/src/ngx_http_hello_module/

Make

Make install

 

配置/usr/local/nginx/conf/nginx.conf

location /hello{

                hello_string "hi";

                hello_counter on;

}

 

启动nginx,访问100.100.217.52/hello

可以看到显示hello world

 

 

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>


typedef struct
{
        ngx_str_t hello_string;
        ngx_int_t hello_counter;
}ngx_http_hello_loc_conf_t;

static ngx_int_t ngx_http_hello_init(ngx_conf_t *cf);

static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf);

static char *ngx_http_hello_string(ngx_conf_t *cf, ngx_command_t *cmd,
        void *conf);
static char *ngx_http_hello_counter(ngx_conf_t *cf, ngx_command_t *cmd,
        void *conf);

//模块配置指令
static ngx_command_t ngx_http_hello_commands[] = {
   {
                ngx_string("hello_string"),
                NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,
                ngx_http_hello_string,
                NGX_HTTP_LOC_CONF_OFFSET,
                offsetof(ngx_http_hello_loc_conf_t, hello_string),
                NULL },

        {
                ngx_string("hello_counter"),
                NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
                ngx_http_hello_counter,
                NGX_HTTP_LOC_CONF_OFFSET,
                offsetof(ngx_http_hello_loc_conf_t, hello_counter),
                NULL },

        ngx_null_command
};


/*
static u_char ngx_hello_default_string[] = "Default String: Hello, world!";
*/
static int ngx_hello_visited_times = 0;

//模块上下文结构
static ngx_http_module_t ngx_http_hello_module_ctx = {
        NULL,                          /* preconfiguration */
        ngx_http_hello_init,           /* postconfiguration *///handler挂载

        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 */
        NULL                            /* merge location configuration */
};

//模块定义
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
};

//handler模块处理函数
static ngx_int_t
ngx_http_hello_handler(ngx_http_request_t *r)
{
        ngx_int_t    rc;

        //声明缓冲区 buffer 和 chain link:
        ngx_buf_t   *b;
        ngx_chain_t  out;

        ngx_http_hello_loc_conf_t* my_conf;
        u_char ngx_hello_string[1024] = {0};
        ngx_uint_t content_length = 0;

        ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "ngx_http_hello_handler is called!");

        my_conf = ngx_http_get_module_loc_conf(r, ngx_http_hello_module);//获取 location 配置,指向调用 ngx_http_get_module_loc_conf 函数即可,该函数传入的参数是 request 结构和自定义的 module 模块
        if (my_conf->hello_string.len == 0 )
        {
                ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "hello_string is empty!");
                return NGX_DECLINED;
        }


        if (my_conf->hello_counter == NGX_CONF_UNSET
                || my_conf->hello_counter == 0)
        {
                ngx_sprintf(ngx_hello_string, "%s", my_conf->hello_string.data);
        }
        else
        {
                ngx_sprintf(ngx_hello_string, "%s Visited Times:%d", my_conf->hello_string.data,
                        ++ngx_hello_visited_times);
        }
        ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "hello_string:%s", ngx_hello_string);
        content_length = ngx_strlen(ngx_hello_string);

        /* we response to 'GET' and 'HEAD' requests only */
        if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
                return NGX_HTTP_NOT_ALLOWED;
        }

        /* discard request body, since we don't need it here */
        rc = ngx_http_discard_request_body(r);

        if (rc != NGX_OK) {
                return rc;
        }

        /* set the 'Content-type' header */
        /*
         *r->headers_out.content_type.len = sizeof("text/html") - 1;
         *r->headers_out.content_type.data = (u_char *)"text/html";
         */
        ngx_str_set(&r->headers_out.content_type, "text/html");


        /* send the header only, if the request type is http 'HEAD' 发送响应的header头部*/
        if (r->method == NGX_HTTP_HEAD) {
                r->headers_out.status = NGX_HTTP_OK;
                r->headers_out.content_length_n = content_length;

                return ngx_http_send_header(r);
        }
        //发送响应的body包体
        /* allocate a buffer for your response body 分配缓冲区 buffer,使响应数据指向它*/
        b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
        if (b == NULL) {
                return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }

        /* attach this buffer to the buffer chain 模块挂载到 chain link 上*/
        out.buf = b;
        out.next = NULL;

        /* adjust the pointers of the buffer */
        b->pos = ngx_hello_string;
        b->last = ngx_hello_string + content_length;
        b->memory = 1;    /* this buffer is in memory */
        b->last_buf = 1;  /* this is the last buffer in the buffer chain */

        /* set the status line */
        r->headers_out.status = NGX_HTTP_OK;
        r->headers_out.content_length_n = content_length;

        /* send the headers of your response */
        rc = ngx_http_send_header(r);

        if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
                return rc;
        }

        /* send the buffer chain of your response发送包体 */
        return ngx_http_output_filter(r, &out);
}

static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf)
{
        ngx_http_hello_loc_conf_t* local_conf = NULL;
        local_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_loc_conf_t));
        if (local_conf == NULL)
        {
                return NULL;
        }

        ngx_str_null(&local_conf->hello_string);
        local_conf->hello_counter = NGX_CONF_UNSET;

        return local_conf;
}

/*
static char *ngx_http_hello_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
        ngx_http_hello_loc_conf_t* prev = parent;
        ngx_http_hello_loc_conf_t* conf = child;

        ngx_conf_merge_str_value(conf->hello_string, prev->hello_string, ngx_hello_default_string);
        ngx_conf_merge_value(conf->hello_counter, prev->hello_counter, 0);

        return NGX_CONF_OK;
}*/

static char *
ngx_http_hello_string(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{

        ngx_http_hello_loc_conf_t* local_conf;
 

        local_conf = conf;
        char* rv = ngx_conf_set_str_slot(cf, cmd, conf);//读取字符串类型的参数

        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "hello_string:%s", local_conf->hello_string.data);//打印出错信息

        return rv;
}


static char *ngx_http_hello_counter(ngx_conf_t *cf, ngx_command_t *cmd,
        void *conf)
{
        ngx_http_hello_loc_conf_t* local_conf;

        local_conf = conf;

        char* rv = NULL;

        rv = ngx_conf_set_flag_slot(cf, cmd, conf);


        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "hello_counter:%d", local_conf->hello_counter);
        return rv;
}

//handler挂载函数
static ngx_int_t
ngx_http_hello_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_CONTENT_PHASE].handlers);
        if (h == NULL) {
                return NGX_ERROR;
        }

        *h = ngx_http_hello_handler;

        return NGX_OK;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Windows 下给 nginx 添加 gzip_static_module 模块,需要重新编译 nginx。以下是具体步骤: 1. 安装 Visual Studio 首先需要安装 Microsoft Visual Studio,官网下载地址:https://visualstudio.microsoft.com/zh-hans/downloads/ 2. 下载 nginx 源码 从 nginx 官网下载最新的 nginx 源码:http://nginx.org/en/download.html 3. 解压 nginx 源码 解压下载的 nginx 源码到任意目录,如:C:\nginx-1.20.1 4. 安装 Perl nginx 的编译过程需要用到 Perl,需要安装 Perl。官网下载地址:https://www.perl.org/get.html 5. 安装 PCRE PCRE 是一个支持正则表达式的库,nginx 的编译过程需要用到。可以从这里下载最新的 PCRE:https://ftp.pcre.org/pub/pcre/ 将解压后的文件夹复制到 C 盘根目录,如:C:\pcre-8.44 6. 安装 zlib zlib 是一个压缩库,nginx 的编译过程需要用到。可以从这里下载最新的 zlib:https://zlib.net/ 将解压后的文件夹复制到 C 盘根目录,如:C:\zlib-1.2.11 7. 编辑 nginx 配置文件 在 C:\nginx-1.20.1 目录下找到 conf 目录,编辑 nginx.conf 文件,在 http 模块中添加以下内容: ``` gzip_static on; ``` 8. 编译 nginx 打开 Visual Studio,选择 “文件” -> “打开” -> “项目/解决方案”,找到 C:\nginx-1.20.1 目录下的 nginx.sln 文件,双击打开。 在 Visual Studio 中选择 “生成” -> “生成解决方案”,等待编译完成。 9. 替换旧的 nginx.exe 编译完成后,会在 C:\nginx-1.20.1\objs 目录下生成新的 nginx.exe 文件。将该文件替换旧的 nginx.exe 文件,重启 nginx 服务器即可。 注意:以上步骤仅适用于 Windows 平台下的编译和安装,Linux 平台下的编译和安装方法略有不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值