黑帽联盟

 找回密码
 会员注册
查看: 1013|回复: 0
打印 上一主题 下一主题

[基础服务] 通过Nginx自定义响应Header头信息

[复制链接]

852

主题

38

听众

3180

积分

管理员

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    奋斗
    昨天 13:06
  • 签到天数: 1541 天

    [LV.Master]伴坛终老

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

    Nginx 使用 ngx_headers_more 模块来增加、删除出站、入站的 Header 信息。默认该模块没有加入到 Nginx 的源码中,要想使用相关功能需要在编译 Nginx 时加入该模块。本人服务器中的 Nginx 在编译时没有加入该模块,使用 -V 查看当前 Nginx 的编译参数:
    [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
    重新编译 Nginx 前,请求 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 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 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 删除,以及修改Server的值也是可以的:
    more_clear_headers 'X-Powered-By';

    more_clear_headers 'WP-Super-Cache';

    more_set_headers 'test';



    [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: test

    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 ~]#
    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    勿忘初心,方得始终!
    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部