通過(guò)Nginx定義Header頭信息的實(shí)現(xiàn)步驟
通過(guò)修改nginx的conf文件,輕松達(dá)到自定義HTTP Header的目的。
Nginx 使用 ngx_headers_more 模塊來(lái)增加、刪除出站、入站的 Header 信息。默認(rèn)該模塊沒(méi)有加入到 Nginx 的源碼中,要想使用相關(guān)功能需要在編譯 Nginx 時(shí)加入該模塊。本人服務(wù)器中的 Nginx 在編譯時(shí)沒(méi)有加入該模塊,使用 -V 查看當(dāng)前 Nginx 的編譯參數(shù):
[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 ~]#
從官網(wǎng)下載模塊:
[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 前,請(qǐng)求 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 ~]#
現(xiàn)在重新編譯 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 已重新編譯并平滑升級(jí)成功。
在 Nginx 的配置文件中加入代碼,將之前請(qǐng)求網(wǎng)站返回 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
再次請(qǐng)求查看效果:
[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 ~]#
經(jīng)測(cè)試已成功將請(qǐng)求返回中的 Header 指定信息刪除。想了解 ngx_headers_more 的其他功能請(qǐng)?jiān)L問(wèn)項(xiàng)目官網(wǎng)。
到此這篇關(guān)于通過(guò)Nginx定義Header頭信息的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)Nginx定義Header頭信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx部署vue頁(yè)面白屏或刷新404問(wèn)題解決
最近部署vue項(xiàng)目后發(fā)現(xiàn)刷新頁(yè)面會(huì)404,本文就來(lái)介紹一下nginx部署vue頁(yè)面白屏或刷新404問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Nginx+tomcat負(fù)載均衡集群的實(shí)現(xiàn)方法
這篇文章主要介紹了Nginx+tomcat負(fù)載均衡集群,的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Centos7.x下Nginx安裝及SSL配置與常用命令詳解
這篇文章主要介紹了Centos7.x下Nginx安裝及SSL配置與常用命令詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02nginx配置proxy_pass后返回404問(wèn)題以及Nginx host相關(guān)變量的說(shuō)明
這篇文章主要介紹了nginx配置proxy_pass后返回404問(wèn)題以及Nginx host相關(guān)變量的說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01