怎么通过Nginx定义Header头信息

其他教程   发布日期:2025年02月21日   浏览次数:263

这篇文章主要介绍了怎么通过Nginx定义Header头信息的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么通过Nginx定义Header头信息文章都会有所收获,下面我们一起来看看吧。

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

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

  1. [root@z-dig ~]# nginx -V
  2. nginx version: www.z-dig.com
  3. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
  4. built with OpenSSL 1.0.1e-fips 11 Feb 2013
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/usr/local/nginx --user=www --group=www
  7. --with-http_ssl_module --with-http_stub_status_module
  8. [root@z-dig ~]#

从官网下载模块:

  1. [root@z-dig ~]# cd /usr/local/src/
  2. [root@z-dig src]# wget https://codeload.github.com/openresty/headers-more-nginx-module/zip/master
  3. -O ./headers-more-nginx-module-master.zip
  4. [root@z-dig src]# unzip headers-more-nginx-module-master.zip

重新编译 Nginx 前,请求 www.z-dig.com 的 Header 信息:

  1. [root@KVM ~]# curl -I www.z-dig.com
  2. HTTP/1.1 200 OK
  3. Server: www.z-dig.com
  4. Date: Sat, 23 Apr 2016 11:25:15 GMT
  5. Content-Type: text/html; charset=UTF-8
  6. Connection: keep-alive
  7. X-Powered-By: PHP/5.6.17
  8. Vary: Accept-Encoding, Cookie
  9. Cache-Control: max-age=3, must-revalidate
  10. WP-Super-Cache: Served supercache file from PHP
  11. [root@KVM ~]#

现在重新编译 Nginx ,平滑更新:

  1. [root@z-dig ~]# cd /usr/local/src/nginx
  2. [root@z-dig nginx]# make clean
  3. rm -rf Makefile objs
  4. [root@z-dig nginx]#./configure --prefix=/usr/local/nginx --user=www --group=www
  5. --with-http_ssl_module --with-http_stub_status_module
  6. --add-module=/usr/local/src/headers-more-nginx-module-master
  7. [root@z-dig nginx]# make
  8. [root@z-dig nginx]# make install
  9. [root@z-dig nginx]# kill -s USR2 `cat /usr/local/nginx/logs/nginx.pid`
  10. [root@z-dig nginx]# ps -ef|grep nginx
  11. root 2017 1 0 Apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  12. www 2018 2017 0 Apr21 ? 00:00:30 nginx: worker process
  13. root 21717 2017 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  14. www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process
  15. root 21856 18312 0 19:45 pts/2 00:00:00 grep nginx
  16. [root@z-dig nginx]# kill -s WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin`
  17. [root@z-dig nginx]# ps -ef|grep nginx
  18. root 2017 1 0 Apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  19. root 21717 2017 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  20. www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process
  21. root 21943 18312 0 19:49 pts/2 00:00:00 grep nginx
  22. [root@z-dig nginx]# kill -s QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
  23. [root@z-dig nginx]# ps -ef|grep nginx
  24. root 21717 1 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  25. www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process
  26. root 22050 18312 0 19:54 pts/2 00:00:00 grep nginx
  27. [root@z-dig nginx]#

到此 Nginx 已重新编译并平滑升级成功。

在 Nginx 的配置文件中加入代码,将之前请求网站返回 Header 中的 X-Powered-By 和 WP-Super-Cache 删除:

  1. more_clear_headers 'X-Powered-By';
  2. more_clear_headers 'WP-Super-Cache';
  3. [root@z-dig ~]# nginx -t
  4. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  5. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  6. [root@z-dig ~]# nginx -s reload

再次请求查看效果:

  1. [root@KVM ~]# curl -I www.z-dig.com
  2. HTTP/1.1 200 OK
  3. Server: www.z-dig.com
  4. Date: Sat, 23 Apr 2016 12:03:04 GMT
  5. Content-Type: text/html; charset=UTF-8
  6. Connection: keep-alive
  7. Vary: Accept-Encoding, Cookie
  8. Cache-Control: max-age=3, must-revalidate
  9. [root@KVM ~]#

经测试已成功将请求返回中的 Header 指定信息删除。想了解 ngx_headers_more 的其他功能请访问项目官网。

以上就是怎么通过Nginx定义Header头信息的详细内容,更多关于怎么通过Nginx定义Header头信息的资料请关注九品源码其它相关文章!