Nginx http_addition_filter_module 响应内容中追加内容

addition_filter_module


The ngx_http_addition_module module is a filter that adds text before and after a response. This module is not built by default, it should be enabled with the --with-http_addition_module configuration parameter.

Example Configuration

./configure --prefix=/usr/local/nginx  --with-http_addition_module 

location / {
    add_before_body /before_action;
    add_after_body  /after_action;
}

ngx_http_addition_filter_module 在响应之前或者之后追加文本内容,比如想在站点底部追加一个 js 或者 css,可以使用这个模块来实现,这个模块和淘宝开发的 nginx footer 模块有点类似,但是还是有不同. 这个模块需要依赖子请求, nginx footer 依赖 nginx 写死的配置.

这个模块可以在我们原始返回给用户的响应中,响应之前和之后添加一些不同的内容。添加的方式是在响应的之前或者响应之后添加内容,而不是修改响应的本身。添加内容的方式是通过添加子请求,根据这个子请求返回的响应再添加到原始响应的前和后

ngx_http_addition_filter_module默认是未编译进Nginx的,需要通过with指令将其启用。

 

addition模块的指令


语法: add_before_body uri;
默认值: —
配置段: http, server, location
发起一个子请求,请求给定的 uri,并且将内容追加到主题响应的内容之前。

语法: add_after_body uri;
默认值: —
配置段: http, server, location
发起一个子请求,请求给定的 uri,并且将内容追加到主题响应的内容之后。

语法: addition_types mime-type …;
默认值: addition_types text/html;
配置段: http, server, location
这个指令在 0.7.9 开始支持,指定需要被追加内容的 MIME 类型,默认为“ text/html”,如果制定为*,那么所有的

 

举例说明


第一个例子:

[root@www nginx-1.16.1]# cat objs/ngx_modules.c | grep http_addition_filter_module 
extern ngx_module_t  ngx_http_addition_filter_module;
    &ngx_http_addition_filter_module,
    "ngx_http_addition_filter_module",

[root@www ]# mkdir -p /data/www/test
[root@www ]# echo "body" >> /data/www/test/a.txt 
server {
        listen       80;
        server_name  www.test.com;
        charset utf-8;
        root /data/www/test;
        location /{
       # add_before_body /before_action;   #先注释掉来看看
       # add_after_body /after_action;
       # addition_types *;
        }
        
        location /before_action{
        return 200 'new content before\n';
        }

        location /after_action{
        return 200 'new content after\n';
        }
     }

[root@www ]# curl www.test.com/a.txt  --这是不使用add指令返回结果
body


-----------------------------------------------------------------------------------------

location /{
       add_before_body /before_action;  
       add_after_body /after_action;
       addition_types *;
        }

 location /before_action{    #这个url可以使用反向代理去访问第三方服务,这里为了简单使用简单的return指令返回
        return 200 'new content before\n';
        }

        location /after_action{
        return 200 'new content after\n';
        }

[root@www nginx-1.16.1]# curl www.test.com/a.txt  --可以看到在内容前后添加了相应的内容
new content before
body
new content after
第二个例子:来源于网络

配置nginx.conf:

server {
listen 80;
server_name www.ttlsa.com;
root /data/site/www.ttlsa.com;
location / {
add_before_body /2013/10/header.html;
add_after_body /2013/10/footer.html;
}
}


测试:

以下三个文件,对应请求的主体文件和 add_before_body、 add_after_body 对应的内容
cat /data/site/test.ttlsa.com/2013/10/20131001_add.html
<html>
<head>
<title>I am title</title>
</head>
<body>
ngx_http_addition_module
</body>
</html>

cat /data/site/test.ttlsa.com/2013/10/header.html
I am header!
cat /data/site/test.ttlsa.com/2013/10/footer.html
footer – ttlsa

访问结果如下,可以看到 20131001_add.html 的顶部和底部分别嵌入了子请求 header.html 和 footer 的内容。
curl test.ttlsa.com/2013/10/20131001_add.html
I am header!
<html>
<head>
<title>I am title</title>
</head>
<body>
ngx_http_addition_module
</body>
</html>
footer - ttlsa

addition 模块与nginx sub 替换响应内容模块应用场景有点相同,具体怎么使用,大家结合实际情况来使用。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值