Nginx自定义HTTP Header头信息方法
借助调整 Nginx 的配置文件,能够便捷地实现 HTTP Header 的自定义定制。
Nginx 依赖 ngx_headers_more 模块来执行进出站 Header 信息的增删操作。该模块并未内置于 Nginx 源代码中,若需使用此功能,必须在编译阶段将其加入。笔者服务器上的 Nginx 在初始编译时未包含此模块,可通过 -V 参数查看当前的编译配置:
[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
在执行重新编译操作前,先对 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 2026 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 2026 1 0 Apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www 2026 2026 0 Apr21 ? 00:00:30 nginx: worker process
root 21717 2026 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 2026 1 0 Apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 21717 2026 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 2026 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 头信息的实现步骤介绍至此,更多相关内容建议搜索本站历史文章或浏览后续推荐内容,感谢支持。