原创文章,转载请指明出处并保留原文url地址
本文主要针对nginx的ngx_http_addition_module module模块做简单介绍,本文具体包括如下指令:
add_before_body,add_after_body,addition_types
ngx_http_addition_module module是个过滤器模块, 在相应数据(response)的前面或者后面添加文本。这个模块不是一个默认内建的模块。模块需要--with-http_addition_module配置参数进行设置后才会被编译。
Example Configuration
location / {
add_before_body /before_action;
add_after_body /after_action;
}
Nginx原文:
Module ngx_http_addition_module
The ngx_http_addition_module module is a filter that adds a 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
location / {
add_before_body /before_action;
add_after_body /after_action;
}
1. add_before_body
syntax: | add_before_body uri; |
default: | — |
context: | http, server, location |
在一个正常请求的相应(response)的body前面添加一个子请求的相应数据。空字符串参数作为一个参数时,将取消从前一个级别配置中继承下来的配置取消。
Nginx原文:
Adds a text returned as a result of processing a given subrequest, before the response body. An empty string ("") as a parameter cancels addition inherited from the previous configuration level.
2. add_after_body
syntax: | add_after_body uri; |
default: | — |
context: | http, server, location |
在一个正常请求的相应(response)的body后面添加一个子请求的相应数据。空字符串参数作为一个参数时,将取消从前一个级别配置中继承下来的配置取消。
Nginx原文:
Adds a text returned as a result of processing a given subrequest, after the response body. An empty string ("") as a parameter cancels addition inherited from the previous configuration level.
3. addition_types
syntax: | addition_types mime-type...; |
default: | addition_types text/html; |
context: | http, server, location |
该指令出现在版本0.7.9。
除了 “text/html”外,允许在相应中添加文本指定MIME类型。 特殊值“*”匹配任何MIME类型(0.8.29)。
Nginx原文:
This directive appeared in version 0.7.9.
Allows to add text in responses with the specified MIME types, in addition to “text/html”. The special value “*” matches any MIME type (0.8.29).
摘要
这个模块可以在当前的location之前或者之后增加别的location。
它作为一个输出过滤器执行,包含到其他location中的主请求和子请求不会被完全缓冲,并且仍然以流的形式传递到客户端,因为最终应答体的长度在传递HTTP头的时候是未知的,HTTP的chunked编码总是在这里使用。
一些限制
在0.8.17版本中如果当前的location请求一个请求自身的子请求,包含的location不会被添加。如以下配置:
location /foo {
add_before_body /bar;
}
location /bar {
add_before_body /baz;
}
连接到/foo的请求并不会将/baz对应的location添加到字段中。
同样注意,在定义需要包含的location时只能使用字符串,而不能使用变量,所以以下配置:
location / {
set $before_action /before_action;
add_before_body $before_action;
}
并不能正常工作(虽然在测试配置文件正确性的时候可以通过)