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

通过修改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 删除:

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 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头信息内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Nginx如何获取自定义请求header头和URL参数详解

    目录 一.获取 header 请求头 二.获取url参数 总结 一.获取 header 请求头 在 ngx_lua 中访问 Nginx 内置变量 ngx.var.http_HEADER 即可获得请求头HEADER的内容. 在 nginx配置中,通过$http_HEADER 即可获得请求头HEADER的内容. 案例: $.ajax({ ....... headers: { Accept: "application/json; charset=utf-8", X-TimerLocal: &

  • 为何要小心Nginx的add_header指令详解

    前言 大家都知道,nginx配置文件通过使用add_header指令来设置response header. 昨天无聊用curl查看一个站点的信息,发现返回的头部与想象中的不一样: HTTP/2 200 date: Thu, 07 Feb 2019 04:26:38 GMT content-type: text/html; charset=UTF-8 vary: Accept-Encoding, Cookie cache-control: max-age=3, must-revalidate la

  • Nginx代理时header头中带"_"信息丢失问题的解决

    前言 开发网关项目时,在请求时往请求头header中放入了签名sign_key信息,在接收请求时再从header中拿出,在本地调试时是可以的,但上线之后通过Nginx代理之后发现拿不到. location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_s

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

    通过修改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

  • Nginx隐藏server头信息的实现

    目录 分析 隐藏版本号 php-fpm服务器隐藏版本号 隐藏Server 分析 上一篇文章我们搭建了Nginx,请求响应头如下 [nginx@node01 sbin]$ curl -I 127.0.0.1:8090 HTTP/1.1 200 OK Server: nginx/1.9.9 Date: Fri, 11 Nov 2022 14:56:38 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 11 Nov

  • Nginx 操作响应头信息的实现

    前置条件:需要编译 ngx_http_headers_module 模块,才支持 header 头信息操作 add_header 意思为将自定义的头信息的添加到响应头,指令为 add_header name value [always];,可以用在 http {}, server {}, location {}, if in location {} 上下文中, 只有当响应状态码等于 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1

  • php的curl携带header请求头信息实现http访问的方法

    导读: curl请求时添加请求头信息可以模拟真人操作,不容易被当成是爬虫机器人(采集),从而可以绕过Incapsula等安全验证机制. 1.首先使用浏览器(示例使用的是火狐浏览器)访问接口网址,使用F12调试,查看请求头信息,如下: 2.实现代码: <?php /** * 开始访问请求 * @param $url * @return bool|string */ function fetch_url($url) { $header = FormatHeader($url); $useragent

  • Asp WinHttp.WinHttpRequest.5.1 对象使用详解 伪造 HTTP 头信息

    由于微软封锁了 XmlHttp 对象,所以无法伪造部分 HTTP 头信息,但是 WinHttp.WinHttpRequest.5.1 对象,它居然用可以成功伪造所有 http 请求的 header 信息! 从msdn得知,WinHttp.WinHttpRequest.5.1 是 msxml 4.0 的底层对象,也就是说 XMLHTTP/ServerXMLHTTP 也是在它的基础上封装而来,WinHttpRequest 的用法与 XmlHttp 大致相同. WaitForResponse 在使用异

  • Nginx隐藏服务器端各类信息的方法

    有时我们不希望有人可以通过一些工具来返回我们服务器的信息,下面我来介绍在nginx中隐藏nginx响应头,修改nginx返回头信息,隐藏php版本号,隐藏服务器信息,同学可参考. 首先隐藏nginx版本信息,只需编辑 nginx.conf 文件 添加一行 server_tokens off; http { include /etc/nginx/mime.types; default_type application/octet-stream; index index.php index.html

  • Django request.META.get()获取不到header头的原因分析

    在使用Django过程中需要开发一些API给其他系统使用,为了安全把Token等验证信息放在header头中. 如何获取: 使用request.META.get("headerkey")来获取 注意: 如果headerkey为auth-token,即headers={'auth-token':'1234'} 应该使用request.META.get("HTTP_AUTH_TOKEN")获取 headerkey中的小写转为大写,横线"-"转为下划线

  • HTTP头信息总结

    本文为多篇"HTTP请求头相关文章"及<HTTP权威指南>一书的阅读后个人汇总整理版,以便于理解. 通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息.客户端向服务器发送一个请求,请求头包含请求的方法.URI.协议版本.以及包含请求修饰符.客户信息和内容的类似于MIME的消息结构.服务器以一个状态行作为响应,相应的内容包括消息协议的版本,成功或者错误编码加上包含服务器信息.实体元信息以及可能的实体内容. Http协议定义了很多与服务器交互的方法,最基本的

随机推荐