目录

一、创建扩展模块目录

二、创建config文件

三、创建ngx_http_hello_module.c文件

四、修改Nginx.conf文件

五、编译Nginx源码

六、浏览器访问

从前面的篇章中,我们基本了解了Nginx的运行原理。Nginx整个框架都是通过模块的方式,对整个系统进行解耦和扩展。在HTTP的http/modules/目录下,有非常多的关于HTTP协议的模块,每个模块都有不同的功能。

接下去我们就需要创建一个自定义的模块。

一、创建扩展模块目录


我们在nginx-1.13.1文件夹下,创建一个/extends/ngx_http_hello_module的目录

二、创建config文件


我们的模块名称为:ngx_http_hello_module
我们需要创建一个config文件,这样Nginx在编译的时候,会将你自定义的模块编译进去。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"

三、创建ngx_http_hello_module.c文件
创建自定义的模块必须遵守Nginx的模块规范,需要定义:ngx_command_t 命令行解析结构
ngx_http_module_t 模块的上下文结构
ngx_module_t 模块结构
如果对这几个数据结构还不熟悉,先参照我之前的文章吧。

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r);static char *
ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);/*** 处理nginx.conf中的配置命令解析* 例如:* location /hello {*    hello* }* 当用户请求:http://127.0.0.1/hello的时候,请求会跳转到hello这个配置上* hello的命令行解析回调函数:ngx_http_hello*/
static ngx_command_t ngx_http_hello_commands[] = {{ngx_string("hello"),NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,ngx_http_hello,NGX_HTTP_LOC_CONF_OFFSET,0,NULL},ngx_null_command
};/*** 模块上下文*/
static ngx_http_module_t ngx_http_hello_module_ctx = { NULL, NULL, NULL, NULL,NULL, NULL, NULL, NULL };/*** 模块的定义*/
ngx_module_t ngx_http_hello_module = {NGX_MODULE_V1,&ngx_http_hello_module_ctx,ngx_http_hello_commands,NGX_HTTP_MODULE,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NGX_MODULE_V1_PADDING
};/*** 命令解析的回调函数* 该函数中,主要获取loc的配置,并且设置location中的回调函数handler*/
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);/* 设置回调函数。当请求http://127.0.0.1/hello的时候,会调用此回调函数 */clcf->handler = ngx_http_hello_handler;return NGX_CONF_OK;
}/*** 模块回调函数,输出hello world*/
static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r) {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;}ngx_str_t type = ngx_string("text/plain");ngx_str_t response = ngx_string("Hello World");r->headers_out.status = NGX_HTTP_OK;r->headers_out.content_length_n = response.len;r->headers_out.content_type = type;rc = ngx_http_send_header(r);if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {return rc;}ngx_buf_t *b;b = ngx_create_temp_buf(r->pool, response.len);if (b == NULL) {return NGX_HTTP_INTERNAL_SERVER_ERROR;}ngx_memcpy(b->pos, response.data, response.len);b->last = b->pos + response.len;b->last_buf = 1;ngx_chain_t out;out.buf = b;out.next = NULL;return ngx_http_output_filter(r, &out);
}

四、修改Nginx.conf文件


我们需要通过http://127.0.0.1/hello 就能访问到我们自定义的hello模块。所以我们需要修改一下Nginx.conf文件:

        #ngx_http_hello_module.clocation /hello {hello;}

五、编译Nginx源码


编译自定义的模块,需要加上:--add_module=模块文件夹

sudo ./configure --prefix=/Users/zhuli/Documents/code/soft/nginx
--add-module=/Users/xxx/Documents/code/soft/nginx-1.13.1/extends/ngx_http_hello_module
sudo make && make install

编译后,生成模块数组文件:objs/ngx_modules.c 里面就包含我们自定义的模块

  • 模块数组*ngx_modules[] 中的值就是通过这个文件初始化的
  • 然后通过这个模块数组对每个模块进行初始化
extern ngx_module_t  ngx_http_limit_req_module;
extern ngx_module_t  ngx_http_geo_module;
extern ngx_module_t  ngx_http_map_module;
extern ngx_module_t  ngx_http_split_clients_module;
extern ngx_module_t  ngx_http_referer_module;
extern ngx_module_t  ngx_http_rewrite_module;
extern ngx_module_t  ngx_http_proxy_module;
extern ngx_module_t  ngx_http_fastcgi_module;
extern ngx_module_t  ngx_http_uwsgi_module;
extern ngx_module_t  ngx_http_scgi_module;
extern ngx_module_t  ngx_http_memcached_module;
extern ngx_module_t  ngx_http_empty_gif_module;
extern ngx_module_t  ngx_http_browser_module;
extern ngx_module_t  ngx_http_upstream_hash_module;
extern ngx_module_t  ngx_http_upstream_ip_hash_module;
extern ngx_module_t  ngx_http_upstream_least_conn_module;
extern ngx_module_t  ngx_http_upstream_keepalive_module;
extern ngx_module_t  ngx_http_upstream_zone_module;
extern ngx_module_t  ngx_http_hello_module;
extern ngx_module_t  ngx_http_write_filter_module;
extern ngx_module_t  ngx_http_header_filter_module;
extern ngx_module_t  ngx_http_chunked_filter_module;
extern ngx_module_t  ngx_http_range_header_filter_module;
extern ngx_module_t  ngx_http_gzip_filter_module;
extern ngx_module_t  ngx_http_postpone_filter_module;
extern ngx_module_t  ngx_http_ssi_filter_module;
extern ngx_module_t  ngx_http_charset_filter_module;
extern ngx_module_t  ngx_http_userid_filter_module;
extern ngx_module_t  ngx_http_headers_filter_module;
extern ngx_module_t  ngx_http_copy_filter_module;
extern ngx_module_t  ngx_http_range_body_filter_module;
extern ngx_module_t  ngx_http_not_modified_filter_module;

六、浏览器访问


在浏览器中输入http://127.0.0.1/hello 就能访问我们自定义的hello模块了。

转载地址:

1. https://initphp.blog.csdn.net/article/details/72912128

Nginx源码分析 - 实战篇 - 编写一个自定义的模块(24)相关推荐

  1. Nginx 源码分析

    1.工程 ngx_conf_file.c ngx_connection.c ngx_cycle.c ngx_file.h ngx_module.c ngx_open_file_cache.h ngx_ ...

  2. nginx源码分析—内存池结构ngx_pool_t及内存管理

    本博客( http://blog.csdn.net/livelylittlefish)贴出作者(阿波)相关研究.学习内容所做的笔记,欢迎广大朋友指正! Content 0.序 1.内存池结构 1.1 ...

  3. nginx源码分析(5)——监听socket初始化

    在nginx源码分析(4)中,看到了nginx的事件模型,但其中没有介绍监听socket的初始化.而对于web server来说,需要通过监听socket来监听客户端的连接等.本篇将会具体介绍这方面的 ...

  4. nginx源码分析之网络初始化

    nginx作为一个高性能的HTTP服务器,网络的处理是其核心,了解网络的初始化有助于加深对nginx网络处理的了解,本文主要通过nginx的源代码来分析其网络初始化. 从配置文件中读取初始化信息 与网 ...

  5. Nginx源码分析:epoll事件处理模块概述

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> 事件处理模块概述 Nginx的高效请求的处理依赖于事件管理机制,本次默认的场景是Linux操 ...

  6. Nginx源码分析:惊群处理与负载均衡

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> Nginx的惊群处理与负载均衡概述 当Nginx工作在master/worker模式下时,就 ...

  7. Nginx源码分析:核心数据结构ngx_cycle_t与内存池概述

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> 核心数据结构与内存池概述 在Nginx中的核心数据结构就是ngx_cycle_t结构,在初始 ...

  8. Nginx源码分析:master/worker工作流程概述

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> Nginx的master与worker工作模式 在生成环境中的Nginx启动模式基本都是以m ...

  9. Nginx源码分析:启动流程

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> nginx简介 Nginx的作为服务端软件,表现的主要特点是更快.高扩展.高可靠性.低内存消 ...

  10. Nginx 源码分析-- 模块module 解析执行 nginx.conf 配置文件流程分析 一

    搭建nginx服务器时,主要的配置文件 nginx.conf 是部署和维护服务器人员经常要使用到的文件, 里面进行了许多服务器参数的设置.那么nginx 以模块 module为骨架的设计下是如何运用模 ...

最新文章

  1. 白话spring依赖注入
  2. java 运算符_java 运算符
  3. python 制作二进制文件数据集(bin)
  4. java.util接口_函数接口– Java 8中java.util.function包中的函数接口
  5. 编译环境与生成环境的JDK版本不一样,报:java.util.zip.ZipException: error in opening zip file
  6. 《Linux 就是这个范儿 - 阅读笔记2》 融于心而表于行(1)
  7. Android中应用程序drawable图片资源占用内存的统计
  8. 将网页和文档的背景改为绿色来保护眼睛
  9. Cocos2d-x在线粒子编辑器
  10. Excel直接将选区转为图片,并另存为文件的操作
  11. R语言:方差分析之单因素方差分析和双因素方差分析
  12. md5加密特征码java,讨论:加密算法特征码及其识别
  13. 一图读懂昇思MindSpore · 图算融合 | 破而后立,晓喻新生
  14. echarts的边框图片之切图(重要)以及公共面板样式的制作
  15. Java的class是什么意思?
  16. python 爬虫小案例 8684网站爬取北京公交路线站点信息。
  17. web资源分享(视频、资料)
  18. 学生成绩字典、获取最高分科目分数python_实例:找出分数最高的学生
  19. c语言 一个数组奇数左边 偶数右边,C语言设计实验报告(第七次)
  20. 鸿蒙分期靠谱吗,花呗借呗“陷阱”正式被确认,这两个禁忌不要触碰,别说没提醒你...

热门文章

  1. HTML如何引入外部JS文件
  2. oracle substr函数
  3. GCD Timer事件的精度
  4. 终端/Shell 快捷键
  5. ORACLE—002:Create创作型
  6. Android反射机制实现与原理
  7. AS/400开发经验点滴(六)如何制作下拉菜单
  8. MySQL 第六次练习(索引)
  9. IDEA 中git使用非默认ssh客户端进行登录
  10. 团队博客-随笔:团队展示 (科利尔拉弗队)