Nginx 过滤模块
HTTP过滤模块的开发步骤
- 确定源代码文件名称;
- 创建config脚本,当执行configure时将该目录添加进去;
- 定义过滤模块,实例化ngx_module_t类型的模块结构;
- 通过设置ngx_module_t结构中的ngx_command_t数组来处理感兴趣的配置项;
- 实现初始化函数,初始化方法就是将ngx_http_output_header_filter_t和ngx_http_output_body_filter_t函数插入到过滤模块链表的首部;
- 实现ngx_http_output_header_filter_pt和ngx_http_output_body_filter_pt函数;
- 编译安装后,修改nginx.conf配置文件中的过滤模块选项,开启与否。
配置脚本
<code>ngx_add HTTP_FILTER_MODULES=<span>"<span>$HTTP_FILTER_MODULES</span> ngx_http_myfilter_module"</span> NGX_ADD>"<span>$NGX_ADDON_SRCS</span><span>$ngx_addon_dir</span>/ngx_http_myfilter_module.c"</code>
模块内容
<code><span>#include <..></..></span><span>#include <..></..></span><span>#include <..></..></span><span>//Declare</span><span>static</span> ngx_int_t ngx_http_myfilter_header_filter(ngx_http_request_t *); <span>static</span> ngx_int_t ngx_http_myfilter_body_filter(ngx_http_request_t*,ngx_chain_t*); <span>static</span> ngx_http_output_header_filter_pt ngx_http_next_header_filter; <span>static</span> ngx_http_output_body_filter_pt ngx_http_next_body_filter; <span>//On/Off</span><span>typedef</span><span>struct</span> { ngx_flag_t enable; }ngx_http_myfilter_conf_t; <span>static</span><span>void</span> *ngx_http_myfilter_create_conf(ngx_conf_t *cf){ ngx_http_myfilter_conf_t *mycf; <span>//Allocate memory</span> mycf = (ngx_http_myfilter_conf_t*)ngx_pcalloc(cf->pool,<span>sizeof</span>(ngx_http_myfilter_conf_t)); <span>if</span>(mycf == <span>NULL</span>) <span>return</span><span>NULL</span>; mycf->enable = NGX_CONF_UNSET; <span>return</span> mycf; } <span>static</span><span>char</span> * ngx_http_myfilter_merge_conf(ngx_conf_t *cf,<span>void</span> *parent,<span>void</span> *child){ ngx_http_myfilter_conf_t *prev = (ngx_http_myfilter_conf_t*)parent; ngx_http_myfilter_conf_t *conf = (ngx_http_myfilter_conf_t*)child; ngx_conf_merge_value(conf->enable,prev->enable,<span>0</span>); <span>return</span> NGX_CONF_OK; } <span>/* ------------------------------------------- */</span><span>//State for prefix</span><span>typedef</span><span>struct</span> { <span>/* add_prefix = * 0 the filter module is off * 1 can add prefix * 2 has been added prefix already */</span> ngx_int_t add_prefix; } ngx_http_myfilter_ctx_t; <span>//Analyse configure</span><span>static</span> ngx_command_t ngx_http_myfilter_commands[]={ {ngx_string(<span>"add_prefix"</span>), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_myfilter_conf_t,enable), <span>NULL</span>}, ngx_null_command }; <span>static</span> ngx_int_t ngx_http_myfilter_init(ngx_conf_t *cf){ <span>//Insert before the first node</span> ngx_http_next_header_filter = ngx_http_top_header_filter; ngx_http_top_header_filter = ngx_http_myfilter_header_filter; ngx_http_next_body_filter = ngx_http_top_body_filter; ngx_http_top_body_filter = ngx_http_myfilter_body_filter; <span>return</span> NGX_OK; } <span>/* ------------------------------------------- */</span><span>//Parse</span><span>static</span> ngx_http_module_t ngx_http_myfilter_module_ctx = { <span>NULL</span>, ngx_http_myfilter_init, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, ngx_http_myfilter_create_conf, ngx_http_myfilter_merge_conf }; <span>/* ------------------------------------------- */</span><span>//Module information</span> ngx_module_t ngx_http_myfilter_module = { NGX_MODULE_V1, &ngx_http_myfilter_module_ctx, ngx_http_myfilter_commands, NGX_HTTP_MODULE, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, NGX_MODULE_V1_PADDING }; <span>/* ------------------------------------------- */</span><span>static</span> ngx_str_t filter_prefix = ngx_string(<span>"[my filter module]"</span>); <span>//Filter to process the header</span><span>static</span> ngx_int_t ngx_http_myfilter_header_filter(ngx_http_request_t *r){ ngx_http_myfilter_ctx_t *ctx; ngx_http_myfilter_conf_t *conf; <span>if</span>(r->headers_out<span>.status</span> != NGX_HTTP_OK){ <span>return</span> ngx_http_next_header_filter(r); } <span>//Get context</span> ctx = ngx_http_get_module_ctx(r,ngx_http_myfilter_module); <span>if</span>(ctx){ <span>return</span> ngx_http_next_header_filter(r); } <span>//Get configure by ngx_http_myfilter_conf_t</span> conf = ngx_http_get_module_loc_conf(r,ngx_http_myfilter_module); <span>if</span>(conf->enable == <span>0</span>){<span>//add_prefix off</span><span>return</span> ngx_http_next_header_filter(r); } <span>//Create ngx_http_myfilter_ctx_t</span> ctx = ngx_pcalloc(r->pool,<span>sizeof</span>(ngx_http_myfilter_ctx_t)); <span>if</span>(ctx == <span>NULL</span>){ <span>return</span> NGX_ERROR; } <span>//add_prefix = 0 express do not add prefix</span> ctx->add_prefix = <span>0</span>; <span>//Set context</span> ngx_http_set_ctx(r,ctx,ngx_http_myfilter_module); <span>//myfilter module only figure out Content-Type="text/plain"</span><span>if</span>(r->headers_out<span>.content_type</span><span>.len</span>>=<span>sizeof</span>(<span>"text/plain"</span>) - <span>1</span> && ngx_strncasecmp(r->headers_out<span>.content_type</span><span>.data</span>,(u_char*)<span>"text/plain"</span>,<span>sizeof</span>(<span>"text/plain"</span>)-<span>1</span>)==<span>0</span>){ <span>//Set add_prefix</span> ctx->add_prefix = <span>1</span>; <span>if</span>(r->headers_out<span>.content_length_n</span>><span>0</span>) r->headers_out<span>.content_length_n</span> += filter_prefix<span>.len</span>; } <span>return</span> ngx_http_next_header_filter(r); } <span>//Filter to process the body</span><span>static</span> ngx_int_t ngx_http_myfilter_body_filter(ngx_http_request_t *r,ngx_chain_t *in){ ngx_http_myfilter_ctx_t *ctx; ctx = ngx_http_get_module_ctx(r,ngx_http_myfilter_module); <span>//If cannot get context or the add_prefix is 0/2,do not process</span><span>if</span>(ctx == <span>NULL</span> || ctx->add_prefix !=<span>1</span>) <span>return</span> ngx_http_next_body_filter(r,in); <span>//To make sure never add prefix again</span> ctx->add_prefix = <span>2</span>; <span>//Get prefix string</span> ngx_buf_t *b = ngx_create_temp_buf(r->pool,filter_prefix<span>.len</span>); b->start = b->pos = filter_prefix<span>.data</span>; b->last = b->pos + filter_prefix<span>.len</span>; <span>//Get and set ngx_chain_t list at the bginning</span> ngx_chain_t *cl = ngx_alloc_chain_link(r->pool); cl->buf = b; cl->next = in; <span>return</span> ngx_http_next_body_filter(r,cl); }</code>
配置文件nginx.conf
在http{}中加上
add_prefix on;
开启过滤模块
编译
<code>./configure --<span>add</span>-<span>module</span>=模块路径/ make make install</code>
HTTP过滤模块也是HTTP模块,所以HTTP模块的书写大致也是这样。
版权声明:Pain is just in your mind.
以上就介绍了Nginx HTTP过滤模块开发,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用