您好,欢迎访问一九零五行业门户网

怎么通过Nginx定义Header头信息

通过修改nginx的conf文件,轻松达到自定义http header的目的。
nginx 使用 ngx_headers_more 模块来增加、删除出站、入站的 header 信息。默认该模块没有加入到 nginx 的源码中,要想使用相关功能需要在编译 nginx 时加入该模块。本人服务器中的 nginx 在编译时没有加入该模块,使用 -v 查看当前 nginx 的编译参数:
[root@z-dig ~]# nginx -vnginx version: www.z-dig.combuilt by gcc 4.4.7 20120313 (red hat 4.4.7-16) (gcc)built with openssl 1.0.1e-fips 11 feb 2013tls sni support enabledconfigure 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.comhttp/1.1 200 okserver: www.z-dig.comdate: sat, 23 apr 2016 11:25:15 gmtcontent-type: text/html; charset=utf-8connection: keep-alivex-powered-by: php/5.6.17vary: accept-encoding, cookiecache-control: max-age=3, must-revalidatewp-super-cache: served supercache file from php[root@kvm ~]#
现在重新编译 nginx ,平滑更新:
[root@z-dig ~]# cd /usr/local/src/nginx[root@z-dig nginx]# make cleanrm -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 nginxroot 2017 1 0 apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginxwww 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/nginxwww 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 nginxroot 2017 1 0 apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginxroot 21717 2017 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginxwww 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 nginxroot 21717 1 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginxwww 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 -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@z-dig ~]# nginx -s reload
再次请求查看效果:
[root@kvm ~]# curl -i www.z-dig.comhttp/1.1 200 okserver: www.z-dig.comdate: sat, 23 apr 2016 12:03:04 gmtcontent-type: text/html; charset=utf-8connection: keep-alivevary: accept-encoding, cookiecache-control: max-age=3, must-revalidate[root@kvm ~]#
经测试已成功将请求返回中的 header 指定信息删除。想了解 ngx_headers_more 的其他功能请访问项目官网。
以上就是怎么通过nginx定义header头信息的详细内容。
其它类似信息

推荐信息