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教程有兴趣的朋友有所帮助。

Springboot内置tomcat禁止不安全HTTP方法1、在tomcat的web.xml中可以配置如下内容让tomcat禁止不安全的HTTP方法/*PUTDELETEHEADOPTIONSTRACEBASIC2、Springboot使用内置tomcat没有web.xml配置文件,可以通过以下配置进行,简单来说就是要注入到Spring容器中@ConfigurationpublicclassTomcatConfig{@BeanpublicEmbeddedServletContainerFacto

1.HttpURLConnection使用JDK原生提供的net,无需其他jar包,代码如下:importcom.alibaba.fastjson.JSON;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.net.HttpURLConnection;

一、前言#ssl写在443端口后面。这样http和https的链接都可以用listen443sslhttp2default_server;server_namechat.chengxinsong.cn;#hsts的合理使用,max-age表明hsts在浏览器中的缓存时间,includesubdomainscam参数指定应该在所有子域上启用hsts,preload参数表示预加载,通过strict-transport-security:max-age=0将缓存设置为0可以撤销hstsadd_head

随着互联网的不断发展和改善,Web服务器在速度和性能上的需求也越来越高。为了满足这样的需求,Nginx已经成功地掌握了HTTP2协议并将其融入其服务器的性能中。HTTP2协议要比早期的HTTP协议更加高效,但同时也存在着特定的安全问题。本文将为您详细介绍如何进行Nginx的HTTP2协议优化和安全设置。一、Nginx的HTTP2协议优化1.启用HTTP2在N

httpkeepalive在http早期,每个http请求都要求打开一个tpcsocket连接,并且使用一次之后就断开这个tcp连接。使用keep-alive可以改善这种状态,即在一次tcp连接中可以持续发送多份数据而不会断开连接。通过使用keep-alive机制,可以减少tcp连接建立次数,也意味着可以减少time_wait状态连接,以此提高性能和提高httpd服务器的吞吐率(更少的tcp连接意味着更少的系统内核调用,socket的accept()和close()调用)。但是,keep-ali

一、urllib概述:urllib是Python中请求url连接的官方标准库,就是你安装了python,这个库就已经可以直接使用了,基本上涵盖了基础的网络请求功能。在Python2中主要为urllib和urllib2,在Python3中整合成了urllib。Python3.x中将urllib2合并到了urllib,之后此包分成了以下四个模块:urllib.request:它是最基本的http请求模块,用来模拟发送请求urllib.error:异常处理模块,如果出现错误可以捕获这些异常urllib

一、概述在实际开发过程中,我们经常需要调用对方提供的接口或测试自己写的接口是否合适。很多项目都会封装规定好本身项目的接口规范,所以大多数需要去调用对方提供的接口或第三方接口(短信、天气等)。在Java项目中调用第三方接口的方式有:1、通过JDK网络类Java.net.HttpURLConnection;2、通过common封装好的HttpClient;3、通过Apache封装好的CloseableHttpClient;4、通过SpringBoot-RestTemplate;二、Java调用第三方

被动检查对于被动健康检查,nginx和nginxplus会在事件发生时对其进行监控,并尝试恢复失败的连接。如果仍然无法恢复正常,nginx开源版和nginxplus会将服务器标记为不可用,并暂时停止向其发送请求,直到它再次标记为活动状态。上游服务器标记为不可用的条件是为每个上游服务器定义的,其中包含块中server指令的参数upstream:fail_timeout-设置服务器标记为不可用时必须进行多次失败尝试的时间,以及服务器标记为不可用的时间(默认为10秒)。max_fails-设置在fai


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),