服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - 通过Nginx定义Header头信息的实现步骤

通过Nginx定义Header头信息的实现步骤

2023-04-14 14:31Linux小百科 Nginx

本文主要介绍了通过Nginx定义Header头信息的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

通过修改nginx的conf文件,轻松达到自定义HTTP Header的目的。

Nginx 使用 ngx_headers_more 模块来增加、删除出站、入站的 Header 信息。默认该模块没有加入到 Nginx 的源码中,要想使用相关功能需要在编译 Nginx 时加入该模块。本人服务器中的 Nginx 在编译时没有加入该模块,使用 -V 查看当前 Nginx 的编译参数:

?
1
2
3
4
5
6
7
8
[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 ~]#

从官网下载模块:

?
1
2
3
4
[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 信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
[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 ,平滑更新:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[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 删除:

?
1
2
3
4
5
6
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

再次请求查看效果:

?
1
2
3
4
5
6
7
8
9
10
[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 的其他功能请访问项目官网。

到此这篇关于通过Nginx定义Header头信息的实现步骤的文章就介绍到这了,更多相关Nginx定义Header头信息内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/yaxuan88521/article/details/130076956

延伸 · 阅读

精彩推荐
  • NginxNginx+Keepalived实现双机热备

    Nginx+Keepalived实现双机热备

    这篇文章主要介绍了Nginx+Keepalived实现双机热备,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    faramita20165842019-12-24
  • Nginx教你在容器中使用nginx搭建上传下载的文件服务器

    教你在容器中使用nginx搭建上传下载的文件服务器

    这篇文章主要介绍了容器中使用nginx搭建支持上传下载的文件服务器,在这需要注意为了让nginx支持文件上传,需要下载并运行带有nginx-upload-module模块的容器...

    witton7702022-08-07
  • Nginx详解Nginx之Location配置(Location匹配顺序)

    详解Nginx之Location配置(Location匹配顺序)

    这篇文章主要介绍了详解Nginx之Location配置(Location匹配顺序),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    RobertoHuang6682020-01-20
  • NginxNginx服务器下配置使用索引目录的教程

    Nginx服务器下配置使用索引目录的教程

    这篇文章主要介绍了Nginx服务器下配置使用索引目录的教程,包括自带的auto_index和使用fancy插件美化的用法,需要的朋友可以参考下 ...

    goldensun5762019-11-10
  • NginxNginx1.8.0版本平滑升级新版本1.9.7

    Nginx1.8.0版本平滑升级新版本1.9.7

    这篇文章主要介绍了Nginx1.8.0版本平滑升级新版本1.9.7的相关资料,需要的朋友可以参考下 ...

    模范生4552019-11-11
  • NginxNginx如何配置Http、Https、WS、WSS的方法步骤

    Nginx如何配置Http、Https、WS、WSS的方法步骤

    这篇文章主要介绍了Nginx如何配置Http、Https、WS、WSS的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    冰河团队9412021-05-30
  • NginxNginx内容缓存以及常见参数配置详析

    Nginx内容缓存以及常见参数配置详析

    这篇文章主要给大家介绍了关于Nginx内容缓存以及常见参数配置的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Nginx具有一定的参考学...

    alpha4552020-01-08
  • NginxNginx服务器https配置的方法示例

    Nginx服务器https配置的方法示例

    这篇文章主要介绍了Nginx服务器https配置的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    行云流水5242019-12-29