通过Nginx定义Header头信息
2017-01-15 22:49
316 查看
导读 | 通过修改nginx的conf文件,轻松达到自定义HTTP Header的目的。 |
[root@z-dig ~]# nginx -V nginx version: www.z-dig.com built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --user=www --group=www \ --with-http_ssl_module --with-http_stub_status_module [root@z-dig ~]#
从官网下载模块:
[root@z-dig ~]# cd /usr/local/src/ [root@z-dig src]# wget 、https://codeload.github.com/openresty/headers-more-nginx-module/zip/master\ -O ./headers-more-nginx-module-master.zip [root@z-dig src]# unzip headers-more-nginx-module-master.zip
重新编译 Nginx 前,请求 www.z-dig.com 的 Header 信息:
[root@KVM ~]# curl -I www.z-dig.com HTTP/1.1 200 OK Server: www.z-dig.com Date: Sat, 23 Apr 2016 11:25:15 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.17 Vary: Accept-Encoding, Cookie Cache-Control: max-age=3, must-revalidate WP-Super-Cache: Served supercache file from PHP [root@KVM ~]#
现在重新编译 Nginx ,平滑更新:
[root@z-dig ~]# cd /usr/local/src/nginx [root@z-dig nginx]# make clean rm -rf Makefile objs [root@z-dig nginx]#./configure --prefix=/usr/local/nginx --user=www --group=www \ --with-http_ssl_module --with-http_stub_status_module \ --add-module=/usr/local/src/headers-more-nginx-module-master [root@z-dig nginx]# make [root@z-dig nginx]# make install [root@z-dig nginx]# kill -s USR2 `cat /usr/local/nginx/logs/nginx.pid` [root@z-dig nginx]# ps -ef|grep nginx root 2017 1 0 Apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 2018 2017 0 Apr21 ? 00:00:30 nginx: worker process root 21717 2017 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process root 21856 18312 0 19:45 pts/2 00:00:00 grep nginx [root@z-dig nginx]# kill -s WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin` [root@z-dig nginx]# ps -ef|grep nginx root 2017 1 0 Apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx root 21717 2017 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process root 21943 18312 0 19:49 pts/2 00:00:00 grep nginx [root@z-dig nginx]# kill -s QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` [root@z-dig nginx]# ps -ef|grep nginx root 21717 1 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process root 22050 18312 0 19:54 pts/2 00:00:00 grep nginx [root@z-dig nginx]#
到此 Nginx 已重新编译并平滑升级成功。
在 Nginx 的配置文件中加入代码,将之前请求网站返回 Header 中的 X-Powered-By 和 WP-Super-Cache 删除:
more_clear_headers 'X-Powered-By'; more_clear_headers 'WP-Super-Cache';
[root@z-dig ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@z-dig ~]# nginx -s reload
再次请求查看效果:
[root@KVM ~]# curl -I www.z-dig.com HTTP/1.1 200 OK Server: www.z-dig.com Date: Sat, 23 Apr 2016 12:03:04 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding, Cookie Cache-Control: max-age=3, must-revalidate [root@KVM ~]#
经测试已成功将请求返回中的 Header 指定信息删除。想了解 ngx_headers_more 的其他功能请访问项目官网。
本文原创地址:http://www.linuxprobe.com/nginx-modify-http-header.html
免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好:http://www.linuxprobe.com/thread
相关文章推荐
- 通过Nginx定义Header头信息
- Oracle数据库通过定义TYPE及Member对象来实现日志信息的分级管理 推荐
- 【转帖】客户端通过 HTTP 请求和响应 的 Header 信息总结
- js通过ajax发送header信息php接收
- 修改Nginx的header信息.txt
- nginx定制header返回信息模块ngx_headers_more
- Nginx通过header转发
- 通过HttpServletRequest获取请求header信息及request.getHeader("referer")的作用
- nginx定制header信息:ngx_headers_more
- Nginx隐藏主机信息,proxy_hide_header 与fastcgi_hide_header
- 线程中通过信号和槽函数传递信息的时候,由于用到了自己定义的参数结构,所以使用qRegisterMetaType
- nginx 安全笔记 (修改nginx的header信息和错误显示版本号)
- 通过AFNetworking下载网络闪屏用的图片并存储到本地同时获取header头相关信息
- Nginx_修改Web服务器头信息(Header)里的Server值[转]
- Nginx隐藏主机信息,proxy_hide_header 与fastcgi_hide_header
- nginx定制header返回信息模块ngx_headers_more
- 定义通用的可通过lambda表达式树来获取属性信息
- 通过AFNetworking下载网络闪屏用的图片并存储到本地同时获取header头相关信息
- 通过cURL获取web页面及http header信息
- 定义通用的可通过lambda表达式树来获取属性信息