openresty headers-more-nginx使用
响应头设置
more_set_headers:设置、修改响应头
语法格式:more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...
* 响应阶段(output-header-filter)设置、修改请求头
上下文环境:http、server、location、location if
Replaces (if any) or adds (if not any) the specified output
headers when the response status code matches the codes specified
by the -s option AND the response content type matches the types
specified by the -t option
* 当响应状态匹配-s、响应类型匹配-t,替换或者修改对应的响应头
If either -s or -t is not specified or has an empty list value,
then no match is required. Therefore, the following directive
set the Server output header to the custom value for any status
code and any content type
more_set_headers "Server: my_server";
* 如果没有设置-s、-t,可修改或者设置所有状态、所有类型的响应头
Existing response headers with the same name are always overridden.
If you want to add headers incrementally, use the standard add_header
directive instead
* 如果响应头存在,使用more_set_headers会修改响应头
* 可用add_header会添加同名响应头
Nginx variables are allowed in header values, But variables
won't work in header keys due to performance considerations
set $my_var "dog";
more_set_headers "Server: $my_var";
* header可以包含nginx变量
* 但是出于性能考虑,不能使用nginx变量做header的key
注意事项
Note that although more_set_headers is allowed in location
if blocks, it is not allowed in the server if blocks
* more_set_headers可以出现在location if中
* 但是不能出现在location if中
# This is NOT allowed!
server {
if ($args ~ 'download') {
more_set_headers 'Foo: Bar';
}
...
}
示例
# 包含多个key、value键值对
more_set_headers 'Foo: bar' 'Baz: bah';
# 包含多个相同的参数
more_set_headers -s 404 -s '500 503' 'Foo: bar';
==> more_set_headers -s '404 500 503' 'Foo: bar';
# header键值对格式
Name: Value
Name:
Name
* Name:、Name ==> 清除header中key为Name的value
响应头清除
more_clear_headers:清除响应头
语法格式:more_clear_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...
* 清除响应阶段(output-header-filter)匹配的header
上下文环境:http, server, location, location if
The wildcard character, *, can also be used at the end of the header
name to specify a pattern. For example, the following directive
effectively clears any output headers starting by "X-Hidden-":
more_clear_headers 'X-Hidden-*';
* 通配符(*)可以用在header name的结尾,
* 如X-Hidden-* ==> 表示删除name以 X-Hidden- 开头的header
示例
# more_set_headers、more_clear_headers 清除响应头
more_clear_headers -s 404 -t 'text/plain' Foo Baz;
==> more_set_headers -s 404 -t 'text/plain' Foo Baz
more_set_headers -s 404 -t 'text/plain' "Foo: " "Baz: ";
请求头设置
more_set_input_headers:设置、修改请求头
语法格式:more_set_input_headers [-r] [-t <content-type list>]... <new-header>...
上下文环境:http, server, location, location if
Very much like more_set_headers except that it operates on input
headers (or request headers) and it only supports the -t option.
* 类似more_set_headers指令,都可对header进行设置、修改
* 但more_set_input_headers指令对请求头操作,并且只支持参数-t
Note that using the -t option in this directive means filtering
by the Content-Type request header, rather than the response header.
* -t参数过滤的是请求头,不是响应头
Behind the scene, use of this directive and its friend
more_clear_input_headers will (lazily) register a rewrite
phase handler that modifies r->headers_in the way you
specify. Note that it always run at the end of the rewrite
phase so that it runs after the standard rewrite module
and works in subrequests as well.
* more_set_input_headers、more_clear_input_headers会注册rewrite phase handler
* 这两个指令会在rewrite phase最后执行
If the -r option is specified, then the headers will be replaced
to the new values only if they already exist.
* 如果指定了-r参数,只有在header存在时,才会替换为新的header
* 如果不存在,不会添加新的header
示例
more_set_input_headers "Server: my_server";
more_set_input_headers -r "Server: my_server";
more_set_input_headers -t "applictaion/json" "Server: my_server";
请求头清除
more_clear_input_headers:清除请求头
语法格式:more_clear_input_headers [-t <content-type list>]... <new-header>...
* 清除匹配的请求头
上下文环境:http, server, location, location if
The wildcard character, *, can also be used at the end of the header
name to specify a pattern. For example, the following directive
effectively clears any input headers starting by "X-Hidden-"
more_clear_input_headers 'X-Hidden-*';
* 通配符(*)可以在header name的结尾使用
* X-Hidden-* ==> 删除以 X-Hidden- 开头的请求头
示例
# more_set_input_headers、more_clear_input_headers 清除请求头
more_clear_input_headers -t 'text/plain' Foo Baz;
==> more_set_input_headers -t 'text/plain' Foo Baz
more_set_input_headers -t 'text/plain' "Foo: " "Baz: ";
注意事项
Unlike the standard headers module, this module does not automatically
take care of the constraint among the Expires, Cache-Control, and
Last-Modified headers. You have to get them right yourself or use
the headers module together with this module.
* 和标准的header模块不同,header_more_nginx模块不关心Expires,
* Cache-Control、Last-Modified这些header
* 需要和标准模块一起使用,手动操作这些header
You cannot remove the Connection response header using this module
because the Connection response header is generated by the standard
ngx_http_header_filter_module in the Nginx core, whose output header
filter runs always after the filter of this module. The only way to
actually remove the Connection header is to patch the Nginx core,
that is, editing the C function ngx_http_header_filter in the src/http
/ngx_http_header_filter_module.c file
* 不能用header_more_nginx模块删除Connection响应头
* Connection响应头是由ngx_http_header_filter模块生成的
* ngx_http_header_filter模块在header_more_nginx模块之后运行
使用示例
default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
location /test {
content_by_lua_block {
local headers = ngx.req.get_headers();
for key,value in pairs(headers) do
ngx.say(key, " ==> ", value);
end
}
}
location /test2 {
more_set_input_headers "name:gtlx" "age:20";
content_by_lua_block {
ngx.say("添加请求头后")
local headers = ngx.req.get_headers();
for key,value in pairs(headers) do
ngx.say(key, " ==> ", value);
end
}
}
location /test3 {
more_clear_input_headers "name";
content_by_lua_block {
ngx.say("删除请求头后")
local headers = ngx.req.get_headers();
for key,value in pairs(headers) do
ngx.say(key, " ==> ", value);
end
}
}
location /test4 {
more_set_headers "name:gtlx" "age:20";
more_clear_headers "name";
content_by_lua_block {
ngx.say("添加、删除响应头")
local headers = ngx.resp.get_headers();
for key,value in pairs(headers) do
ngx.say(key, " ==> ", value);
end
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
创建容器
docker run -it -d --net fixed --ip 172.18.0.100 -p 6002:80 \
-v /Users/huli/lua/openresty/header/default2.conf:/etc/nginx/conf.d/default.conf \
--name open-header lihu12344/openresty
使用测试
# 读取请求头
huli@hudeMacBook-Pro header % curl localhost:6002/test
host ==> localhost:6002
accept ==> */*
user-agent ==> curl/7.77.0
# 添加请求头
huli@hudeMacBook-Pro header % curl localhost:6002/test2
添加请求头后
host ==> localhost:6002
user-agent ==> curl/7.77.0
accept ==> */*
age ==> 20
name ==> gtlx
# 删除请求头
huli@hudeMacBook-Pro header % curl localhost:6002/test3
删除请求头后
host ==> localhost:6002
accept ==> */*
user-agent ==> curl/7.77.0
# 添加、删除响应头
huli@hudeMacBook-Pro header % curl localhost:6002/test4
添加、删除响应头
age ==> 20
connection ==> keep-alive
transfer-encoding ==> chunked
content-type ==> application/octet-stream