阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx编写Hello World模块

142次阅读
没有评论

共计 4292 个字符,预计需要花费 11 分钟才能阅读完成。

编写 HTTP 模块

  • 几个重要组成部分
    ngx_command_t 数组

     

    对于我们在 nginx.conf 中编写的配置项 mytest 来说,nginx 首先会遍历所有的模块(modules),而对于每个模块,会遍历他所对应的 ngx_command_t 数组,试图找到关于我们的配置项 mytest 的解析方式。
    这里编写的 hello 模块的代码部分如下:

    static ngx_command_t  ngx_http_mytest_commands[] =
    {
      {ngx_string("mytest"),
          NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
          ngx_http_mytest,
          NGX_HTTP_LOC_CONF_OFFSET,
          0,
          NULL
      },
      ngx_null_command
    };

    command 中用于处理配置项参数的 set 方法

    static char *
    ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
    {
      ngx_http_core_loc_conf_t  *clcf;
    
      // 首先找到 mytest 配置项所属的配置块,clcf 貌似是 location 块内的数据
    // 结构,其实不然,它可以是 main、srv 或者 loc 级别配置项,也就是说在每个
    //http{}和 server{}内也都有一个 ngx_http_core_loc_conf_t 结构体
      clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    
      //http 框架在处理用户请求进行到 NGX_HTTP_CONTENT_PHASE 阶段时,如果
    // 请求的主机域名、URI 与 mytest 配置项所在的配置块相匹配,就将调用我们
    // 实现的 ngx_http_mytest_handler 方法处理这个请求
      clcf->handler = ngx_http_mytest_handler;
    
      return NGX_CONF_OK;
    }

    关于 ngx_http_conf_get_module_loc_conf 的定义可以参考:http://lxr.nginx.org/source/src/http/ngx_http_config.h#0065 本质:就是设置 ngx_http_mytest_handler, 匹配项被选中的时候,应该如何解析。
    定义 ngx_http_module_t 接口
    这部分的代码,是用于 http 框架的,相当于 http 框架的回掉函数,由于这里并不需要框架做任何操作,所以全部设置成 NULL 即可。

    static ngx_http_module_t  ngx_http_mytest_module_ctx =
    {
      NULL,                              /* preconfiguration */
      NULL,                       /* postconfiguration */
    
      NULL,                              /* create main configuration */
      NULL,                              /* init main configuration */
    
      NULL,                              /* create server configuration */
      NULL,                              /* merge server configuration */
    
      NULL,                   /* create location configuration */
      NULL                    /* merge location configuration */
    };

    定义 mytest 模块
    mytest 模块的详细内容解析

 

 

 

这里的模块只需要设置三个内容:

ngx_module_t  ngx_http_mytest_module =
{
    NGX_MODULE_V1,
    &ngx_http_mytest_module_ctx,           /* module context */
    ngx_http_mytest_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
};

设置完成 ngx_module_t 数组后,mytest 模块在编译的时候,就可以被加入到 ngx_modules 的全局数组中了

处理用户请求的 hello world handler
该方法是配置项匹配之后的处理方法:

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
    // 必须是 GET 或者 HEAD 方法,否则返回 405 Not Allowed
    if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD)))
    {return NGX_HTTP_NOT_ALLOWED;}

    // 丢弃请求中的包体
    ngx_int_t rc = ngx_http_discard_request_body(r);
    if (rc != NGX_OK)
    {return rc;}

    // 设置返回的 Content-Type。注意,ngx_str_t 有一个很方便的初始化宏
//ngx_string,它可以把 ngx_str_t 的 data 和 len 成员都设置好
    ngx_str_t type = ngx_string("text/plain");
    // 返回的包体内容
    ngx_str_t response = ngx_string("Hello World!");
    // 设置返回状态码
    r->headers_out.status = NGX_HTTP_OK;
    // 响应包是有包体内容的,所以需要设置 Content-Length 长度
    r->headers_out.content_length_n = response.len;
    // 设置 Content-Type
    r->headers_out.content_type = type;

    // 发送 http 头部
    rc = ngx_http_send_header(r);
    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)
    {return rc;}

    // 构造 ngx_buf_t 结构准备发送包体
    ngx_buf_t                 *b;
    b = ngx_create_temp_buf(r->pool, response.len);
    if (b == NULL)
    {return NGX_HTTP_INTERNAL_SERVER_ERROR;}
    // 将 Hello World 拷贝到 ngx_buf_t 指向的内存中
    ngx_memcpy(b->pos, response.data, response.len);
    // 注意,一定要设置好 last 指针
    b->last = b->pos + response.len;
    // 声明这是最后一块缓冲区
    b->last_buf = 1;

    // 构造发送时的 ngx_chain_t 结构体
    ngx_chain_t     out;
    // 赋值 ngx_buf_t
    out.buf = b;
    // 设置 next 为 NULL
    out.next = NULL;

    // 最后一步发送包体,http 框架会调用 ngx_http_finalize_request 方法
// 结束请求
    return ngx_http_output_filter(r, &out);
}

将 HTTP 模块编译到 nginx 中

模块的源代码应该和 config 文件放到一个目录下面,然后在编译的时候加入参数,–add-module=PATH。其中 config 文件的内容如下所示:

ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

配置 nginx.conf

location \hello{mytest;}

运行效果如下:

Nginx 编写 Hello World 模块

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

Nginx 403 forbidden 的解决办法  http://www.linuxidc.com/Linux/2017-08/146084.htm

CentOS 7 下 Nginx 服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS 上安装 Nginx 服务器实现虚拟主机和域名重定向  http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux 下安装 PHP 环境并配置 Nginx 支持 php-fpm 模块  http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx 服务的 SSL 认证和 htpasswd 认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

Ubuntu 16.04 上启用加密安全的 Nginx Web 服务器  http://www.linuxidc.com/Linux/2017-07/145522.htm

Linux 中安装配置 Nginx 及参数详解  http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2 下 Nginx+PHP+MySQL+Memcache 缓存服务器安装配置  http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9 编译安装 Nginx1.4.7  http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/147590.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4292字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中