Nginx教程
01、关于 Nginx
02、什么是 Nginx
03、Nginx 的特点
04、初探 Nginx 架构
05、Nginx 基础概念
06、Nginx 基本数据结构
07、Nginx 的配置系统
08、Nginx 的模块化体系结构
09、Nginx 的请求处理
10、Nginx handler 模块简介
11、Nginx 模块的基本结构
12、Nginx handler 模块的基本结构
13、Nginx handler 模块的挂载
14、Nginx handler 的编写步骤
15、Nginx 示例_ hello handler 模块
16、Nginx handler 模块的编译和使用
17、Nginx 更多 handler 模块示例分析
18、Nginx 过滤模块简介
19、Nginx 过滤模块的分析
20、Nginx upstream 模块简介
21、Nginx 负载均衡模块
22、Nginx core 模块
23、Nginx event 模块
24、Nginx 配置文件nginx.conf中文详解
本文档使用 MrDoc 发布
-
+
首页
16、Nginx handler 模块的编译和使用
### **handler 模块的编译和使用** ------------ 模块的功能开发完了之后,模块的使用还需要编译才能够执行,下面我们来看下模块的编译和使用。 ### **config 文件的编写** ------------ 对于开发一个模块,我们是需要把这个模块的 C 代码组织到一个目录里,同时需要编写一个 config 文件。这个 config 文件的内容就是告诉 Nginx 的编译脚本,该如何进行编译。我们来看一下 hello handler module 的 config 文件的内容,然后再做解释。 ```python 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_ADDON_SRCS 这个变量里,依次写进去就可以。 ### **编译** ------------ 对于模块的编译,Nginx 并不像 apache 一样,提供了单独的编译工具,可以在没有 apache 源代码的情况下来单独编译一个模块的代码。Nginx 必须去到 Nginx 的源代码目录里,通过 configure 指令的参数,来进行编译。下面看一下 hello module 的 configure 指令: ```python ./configure --prefix=/usr/local/nginx-1.3.1 --add-module=/home/jizhao/open_source/book_module ``` 我写的这个示例模块的代码和 config 文件都放在`/home/jizhao/open_source/book_module`这个目录下。所以一切都很明了,也没什么好说的了。 ### **使用** ------------ 使用一个模块需要根据这个模块定义的配置指令来做。比如我们这个简单的 hello handler module 的使用就很简单。在我的测试服务器的配置文件里,就是在 http 里面的默认的 server 里面加入如下的配置: ```python location /test { hello_string jizhao; hello_counter on; ``` 当我们访问这个地址的时候, `lynx http://127.0.0.1/test` 的时候,就可以看到返回的结果。 ```python jizhao Visited Times:1 ``` 当然你访问多次,这个次数是会增加的。
李智
2025年3月17日 13:32
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码