首页 后端开发 php教程 Nginx HTTP过滤模块开发

Nginx HTTP过滤模块开发

Aug 08, 2016 am 09:20 AM
conf filter http

Nginx 过滤模块

HTTP过滤模块的开发步骤

  1. 确定源代码文件名称;
  2. 创建config脚本,当执行configure时将该目录添加进去;
  3. 定义过滤模块,实例化ngx_module_t类型的模块结构;
  4. 通过设置ngx_module_t结构中的ngx_command_t数组来处理感兴趣的配置项;
  5. 实现初始化函数,初始化方法就是将ngx_http_output_header_filter_t和ngx_http_output_body_filter_t函数插入到过滤模块链表的首部;
  6. 实现ngx_http_output_header_filter_pt和ngx_http_output_body_filter_pt函数;
  7. 编译安装后,修改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教程有兴趣的朋友有所帮助。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

http状态码520是什么意思 http状态码520是什么意思 Oct 13, 2023 pm 03:11 PM

http状态码520是指服务器在处理请求时遇到了一个未知的错误,无法提供更具体的信息。用于表示服务器在处理请求时发生了一个未知的错误,可能是由于服务器配置问题、网络问题或其他未知原因导致的。通常是由服务器配置问题、网络问题、服务器过载或代码错误等原因导致的。如果遇到状态码520错误,最好联系网站管理员或技术支持团队以获取更多的信息和帮助。

如何使用Nginx Proxy Manager实现HTTP到HTTPS的自动跳转 如何使用Nginx Proxy Manager实现HTTP到HTTPS的自动跳转 Sep 26, 2023 am 11:19 AM

如何使用NginxProxyManager实现HTTP到HTTPS的自动跳转随着互联网的发展,越来越多的网站开始采用HTTPS协议来加密传输数据,以提高数据的安全性和用户的隐私保护。由于HTTPS协议需要SSL证书的支持,因此在部署HTTPS协议时需要有一定的技术支持。Nginx是一款强大且常用的HTTP服务器和反向代理服务器,而NginxProxy

http状态码403是什么 http状态码403是什么 Oct 07, 2023 pm 02:04 PM

http状态码403是服务器拒绝了客户端的请求的意思。解决http状态码403的方法是:1、检查身份验证凭据,如果服务器要求身份验证,确保提供正确的凭据;2、检查IP地址限制,如果服务器对IP地址进行了限制,确保客户端的IP地址被列入白名单或未列入黑名单;3、检查文件权限设置,如果403状态码与文件或目录的权限设置有关,确保客户端具有足够的权限访问这些文件或目录等等。

理解网页重定向的常见应用场景并了解HTTP301状态码 理解网页重定向的常见应用场景并了解HTTP301状态码 Feb 18, 2024 pm 08:41 PM

掌握HTTP301状态码的含义:网页重定向的常见应用场景随着互联网的迅猛发展,人们对网页交互的要求也越来越高。在网页设计领域,网页重定向是一种常见且重要的技术,通过HTTP301状态码来实现。本文将探讨HTTP301状态码的含义以及在网页重定向中的常见应用场景。HTTP301状态码是指永久重定向(PermanentRedirect)。当服务器接收到客户端发

快速应用:PHP 异步 HTTP 下载多个文件的实用开发案例分析 快速应用:PHP 异步 HTTP 下载多个文件的实用开发案例分析 Sep 12, 2023 pm 01:15 PM

快速应用:PHP异步HTTP下载多个文件的实用开发案例分析随着互联网的发展,文件下载功能已成为很多网站和应用程序的基本需求之一。而对于需要同时下载多个文件的场景,传统的同步下载方式往往效率低下且耗费时间。为此,使用PHP异步HTTP下载多个文件成为了一种越来越常见的解决方案。本文将通过一个实际的开发案例,详细分析如何使用PHP异步HTTP

使用http.PostForm函数发送带有表单数据的POST请求 使用http.PostForm函数发送带有表单数据的POST请求 Jul 25, 2023 pm 10:51 PM

使用http.PostForm函数发送带有表单数据的POST请求在Go语言的http包中,可以使用http.PostForm函数发送带有表单数据的POST请求。http.PostForm函数的原型如下:funcPostForm(urlstring,dataurl.Values)(resp*http.Response,errerror)其中,u

http请求415错误解决方法 http请求415错误解决方法 Nov 14, 2023 am 10:49 AM

解决方法:1、检查请求头中的Content-Type;2、检查请求体中的数据格式;3、使用适当的编码格式;4、使用适当的请求方法;5、检查服务器端的支持情况。

C#中常见的网络通信和安全性问题及解决方法 C#中常见的网络通信和安全性问题及解决方法 Oct 09, 2023 pm 09:21 PM

C#中常见的网络通信和安全性问题及解决方法在当今互联网时代,网络通信已经成为了软件开发中必不可少的一部分。在C#中,我们通常会遇到一些网络通信的问题,例如数据传输的安全性、网络连接的稳定性等。本文将针对C#中常见的网络通信和安全性问题进行详细讨论,并提供相应的解决方法和代码示例。一、网络通信问题网络连接中断:网络通信过程中,可能会出现网络连接的中断,这会导致

See all articles