ngx_headers_more是Nginx的一个很不错的模块,ngx_headers_more主要用于添加、设置和清除输入或者输出http header头的信息。下面详细的讲解Nginx ngx_headers_more模块的安装、具体的用法和配置。
1、安装最新的ngx_headers_more压缩包
1 2 3 4 5 6 7
| [root@nginx ~]# cd /usr/local/src/nginx-1.9.9 [root@nginx nginx-1.9.9]# wget https://github.com/openresty/headers-more-nginx-module/archive/v0.29.tar.gz [root@nginx nginx-1.9.9]# tar zxvf headers-more-nginx-module-0.29.tar.gz [root@nginx nginx-1.9.9]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.35 --add-module=../ngx_cache_purge-2.1 --add-module=/usr/local/src/headers-more-nginx-module-0.26 [root@nginx nginx-1.9.9]# make [root@nginx nginx-1.9.9]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak [root@nginx nginx-1.9.9]# cp /usr/local/src/nginx-1.9.9/objs/nginx /usr/local/nginx/sbin/nginx |
2、ngx_headers_more的用法
more_set_headers替换(如有)或增加(如果不是所有)指定的输出header头时响应状态代码与-s选项相匹配和响应的内容类型的-t选项指定的类型相匹配的。
语法:more_set_headers [-t ]… [-s ]… …
默认值:no
配置段:http, server, location, location if
阶段:输出报头过滤器
more_clear_headers清除指定的输出header头。
语法:more_clear_headers [-t ]… [-s ]… …
默认值:no
配置段:http, server, location, location if
阶段:输出报头过滤器
more_set_input_headers设置指定的输入header头,和more_set_headers类似,仅支持-t选项。
语法:more_set_input_headers [-r] [-t ]… …
默认值:no
配置段:http, server, location, location if
阶段: rewrite tail
注意:使用-t选项的是过滤请求头的Content-Type,而不是响应头的。
more_clear_input_headers清除指定输入header头。
语法:more_clear_input_headers [-t ]… …
默认值:no
配置段:http, server, location, location if
阶段: rewrite tail
在nginx.conf配置中的server段,添加清除header信息
1 2 3 4 5 6 7 8 9
| location /api/ { more_clear_headers "X-Powered-By"; more_clear_headers "Server"; more_clear_headers "ETag"; more_clear_headers "Connection"; more_clear_headers "Date"; more_clear_headers "Accept-Ranges"; more_clear_headers "Last-Modified"; } |