流程
HTTP
模块来实现产品功能时,是可以完全享用
Nginx
的优秀设计所带来的、
HTTP
模块
模块要能介入到
HTTP
请求的处理流程中
HTTP
请求,但在开始处理请求前还需要先了解一些Nginx
框架定义的数据结构
Nginx
框架接收、解析后的用户请求信息
包体的形式发送给用户
调用模板
HTTP
模块是如何介入
Nginx
处理用户请求流 程的。下图是一个简化的时序图,这里省略了许多异步调用,忽略了多个不同的
HTTP
处理阶段,仅标识了在一个典型请求的处理过程中主要模块被调用的流程,以此帮助读者理解
HTTP模块如何处理用户请求。
循环语句里反复调用事件模块检测网络事件。
请求时(接收到
SYN
包),将会为它建立
TCP
连 接,
其中最常见的是根据请求的URI
和
nginx.conf
里
location
配置项
模块在处理请求的结束时,大多会向客户端发送响应,此时会自动地依次调
HTTP
过滤模块,每个过滤模块可以根据配置文件决定自己的行为
准备工作
Nginx模块需要使用C(或者C++)语言编写代码来实现,每个模块都要有自己的名字。 按照Nginx约定俗成的命名规则,我们把第一个HTTP模块命名为ngx_http_mytest_module。由 于第一个模块非常简单,一个C源文件就可以完成
为了做到跨平台,Nginx定义、封装了一些基本的数据结构。由于Nginx对内存分配比较“吝啬”(只有保证低内存消耗,才可能实现十万甚至百万级别的同时并发连接数),所以 这些Nginx数据结构天生都是尽可能少占用内存。(看我nginx源码解析即可)
如何将自己的HTTP模块编译进Nginx
方法一 config文件
提供了一种简单的方式将第三方的模块编译到
Nginx
中。首先把源代码文件全部放
Nginx
如何编译本模块,这个文件名
config
config写法
config文件其实是一个可执行的Shell脚本。如果只想开发一个HTTP模块,那么config文 件中需要定义以下3个变量
"$HTTP_MODULES ngx_http_mytest_module"
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_
下面完整地解释一下configure上文提到的config文件配合起来把定制的 第三方模块加入到Nginx中的:
configure --add-module=PATh(config文件)
"ngx_http_uwsgi_module",
"ngx_http_scgi_module",
"ngx_http_memcached_module",
"ngx_http_empty_gif_module",
"ngx_http_browser_module",
"ngx_http_upstream_hash_module",
"ngx_http_upstream_ip_hash_module",
"ngx_http_upstream_least_conn_module",
"ngx_http_upstream_keepalive_module",
"ngx_http_upstream_zone_module",
"ngx_http_mytest_mudule",//我们自己的
"ngx_http_write_filter_module",
"ngx_http_header_filter_module",
"ngx_http_chunked_filter_module",
"ngx_http_range_header_filter_module",
"ngx_http_gzip_filter_module",
"ngx_http_postpone_filter_module",
"ngx_http_ssi_filter_module",
"ngx_http_charset_filter_module",
"ngx_http_userid_filter_module",
"ngx_http_headers_filter_module",
"ngx_http_copy_filter_module",
"ngx_http_range_body_filter_module",
"ngx_http_not_modified_filter_module",
在执行configure–add-module=PATH命令时,PATH就是第三方模块所在的路径。在configure中,通过auto/options脚本设置了NGX_ADDONS变量
方法二 修改makefile
数组中各个
objs/ngx_modules.c和objs/Makefile文件直接进行修改。
objs/ngx_modules.c
时,首先要添加新增的第三方模块的声明,如下所示:
extern ngx_module_t ngx_http_mytest_module;
ngx_modules
数组中
ngx_module_t *ngx_modules[] = {
…
&ngx_http_upstream_ip_hash_module,
&ngx_http_mytest_module,
&ngx_http_write_filter_module,
…
NULL
};
note:
objs/Makefile
时需要增加编译源代码的部分,例如:
objs/addon/httpmodule/ngx_http_mytest_module.o: $(ADDON_DEPS) \
../sample/httpmodule// ngx_http_mytest_module.c
$(CC) -c $(CFLAGS) $(ALL_INCS) \
-o objs/addon/httpmodule/ngx_http_mytest_module.o \
../sample/httpmodule// ngx_http_mytest_module.c
Nginx
中,例如:
objs/nginx: objs/src/core/nginx.o \
...
objs/addon/httpmodule/ngx_http_mytest_module.o \
objs/ngx_modules.o
$(LINK) -o objs/nginx \
objs/src/core/nginx.o \
...
objs/addon/httpmodule/ngx_http_mytest_module.o \
objs/ngx_modules.o \
-lpthread -lcrypt -lpcre -lcrypto -lcrypto -lz
推荐方法一:因为请慎用这种直接修改Makefile和ngx_modules.c的方法,不正确的修改可能导致Nginx工作 不正常。
拓展知识
关于利用configure过程中发生了什么
结果:
creating objs/Makefile
auto/make: line 420: syntax error near unexpected token `then'
auto/make: line 420: ` if{$ext="cpp"};then'
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
Nginx源码解析–configure_编程界的谢菲尔德的博客-CSDN博客)添加进去
. auto/modules
. auto/make
2.在configure命令执行到auto/modules脚本时,将在生成的ngx_modules.c文件中加入定制的
if test -n "$NGX_ADDONS"; then
echo configuring additional modules
for ngx_addon_dir in $NGX_ADDONS
do
echo "adding module in $ngx_addon_dir"
if test -f $ngx_addon_dir/config; then
#在这里执行自定义的
config脚本
. $ngx_addon_dir/config
echo " + $ngx_addon_name was configured"
else
echo "$0: error: no $ngx_addon_dir/config was found"
exit 1
fi
done
fi
$NGX_ADDONS
可以包含多个目录,对于每个目录,如果其中存在
config
文
config
中重新定义的变量都会生效。之后,
auto/modules
脚本开始
ngx_modules.c
文件,这个文件的关键点就是定义了
ngx_module_t*ngx_modules[]
数组,这
Nginx
中的所有模块。
Nginx
在初始化、处理请求时,都会循环访问
ngx_modules
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/129621.html