科技匠|nginx常见模块headers-more,set-misc

nginx–常见模块,headers-more-nginx and set-misc-nginx 等nginx module : headers-more-nginxinstall and overviewngx_http_headers_module是在Nginx编译时默认自带的模块 , 主要包含add_header和expires两个指令 。 之前有专门的文档介绍过该模板的配置与使用;文章链接:
而ngx_http_headers_module 的2个主要指令(add_header, expires) 中的add_header指令 , 只适合用来添加响应头 , 如需对HTTP请求头进行处理 , 可以使用第三方模块headers-more-nginx , 它可以用来添加、删除、修改HTTP请求头和响应头 。 该模块地址为https://github.com/openresty/headers-more-nginx-module.git , 其安装方式如下:
科技匠|nginx常见模块headers-more,set-misc[root@network-test nginx]# lltotal 1036drwxr-xr-x 6 root root4096 Aug 24 10:59 headers-more-nginx-module-rw-r--r-- 1 root root 1048727 Aug 11 23:03 nginx-1.19.2.tar.gz[root@network-test nginx]# # git clone https://github.com/openresty/headers-more-nginx-module.git[root@network-test nginx]# # wget [root@network-test nginx]# [root@network-test nginx]# tar -xf nginx-1.19.2.tar.gz[root@network-test nginx]# cd nginx-1.19.2/[root@network-test nginx-1.19.2]# ./configure --prefix=/usr/local/nginx --add-module=../headers-more-nginx-module[root@network-test nginx-1.19.2]# make # 上述配置等同于add_header Cache-Control 1000 always;# 作用是无论返回的状态码是什么都会输出响应头 。 初始header;
科技匠|nginx常见模块headers-more,set-misc添加nginx 配置 more_set_headers 'Cache-Control : 1000';
科技匠|nginx常见模块headers-more,set-misc验证已经添加成功:
科技匠|nginx常见模块headers-more,set-misc实例2:
more_set_headers -s '200 301' 'Cache-Control:1000';上述配置等同于add_header Cache-Control 1000; , 表示当状态码是200或301时才输出响应头 , 从而加强对响应头的控制 。
当后端服务器返回的响应头和使用more_set_headers指令时相同时 , 响应头的值会被more_set_headers替换掉 , 这样就不会同时出现两个相同的响应头了 。
headers-more-nginx模块常用的操作指令还有很多 , 例如more_set_headers、more_clear_headers、more_set_input_headers、more_clear_input_headers等 。
科技匠|nginx常见模块headers-more,set-misc
科技匠|nginx常见模块headers-more,set-miscDetail1.根据HTTP状态控制响应头
指令:more_set_headers
语法:more_set_headers [-t ] … [ -s]…
默认值:无
环境:http、server、location、location if
执行阶段:output-header-filter
示例:more_set_headers -s 404 -s ‘500 502’ ‘Result:error’ ‘F:X-re’;


推荐阅读