如何使用Nginx定义Header头信息

发布时间:2022-02-16 15:45:11 作者:iii
来源:亿速云 阅读:211

这篇文章主要介绍“如何使用Nginx定义Header头信息”,在日常操作中,相信很多人在如何使用Nginx定义Header头信息问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用Nginx定义Header头信息”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

如何使用Nginx定义Header头信息

本人服务器中的 Nginx 在编译时没有加入该模块,使用 -V 查看当前 Nginx 的编译参数:

[root@z-dig ~]# nginx -Vnginx 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.comHTTP/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 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/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 nginxroot      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 nginxroot     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 -tnginx: 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.comHTTP/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 指定信息删除。

到此,关于“如何使用Nginx定义Header头信息”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. nginx缓存和自定义header
  2. Curl发送header头信息

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx header

上一篇:nginx的access_log日志怎么设置

下一篇:Nginx怎么设置404页面

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》