溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Nginx定義Header頭信息

發(fā)布時(shí)間:2022-02-16 15:45:11 來源:億速云 閱讀:225 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“如何使用Nginx定義Header頭信息”,在日常操作中,相信很多人在如何使用Nginx定義Header頭信息問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用Nginx定義Header頭信息”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

如何使用Nginx定義Header頭信息

本人服務(wù)器中的 Nginx 在編譯時(shí)沒有加入該模塊,使用 -V 查看當(dāng)前 Nginx 的編譯參數(shù):

[root@z-dig ~]# nginx -Vnginx 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 前,請求 www.z-dig.com 的 Header 信息:

[root@KVM ~]# curl -I www.z-dig.comHTTP/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 cleanrm -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 nginxroot      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 nginxroot      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 nginxroot     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 的配置文件中加入代碼,將之前請求網(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 -tnginx: 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.comHTTP/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)測試已成功將請求返回中的 Header 指定信息刪除。

到此,關(guān)于“如何使用Nginx定義Header頭信息”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI