溫馨提示×

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

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

Nginx服務(wù)器的nginx-http-footer-filter模塊怎么配置

發(fā)布時(shí)間:2022-04-29 16:36:14 來源:億速云 閱讀:136 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“Nginx服務(wù)器的nginx-http-footer-filter模塊怎么配置”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Nginx服務(wù)器的nginx-http-footer-filter模塊怎么配置”吧!

1. nginx-http-footer-filter到底是做什么的?
說白了,就是在請(qǐng)求的頁面底部插入你要插入的代碼。
2. 我們能用nginx-http-footer-filter來做什么?
1、統(tǒng)一追加js代碼用于統(tǒng)計(jì)(我是這么想的)
2、底部追加響應(yīng)這個(gè)請(qǐng)求的realsver(后端真實(shí)服務(wù)器)信息,便于系統(tǒng)管理員排查故障.
3、你管理著數(shù)量龐大的虛擬主機(jī),在所有web后面追加你的廣告代碼,黑鏈?zhǔn)裁吹模ê軣o恥)
4、舉一反三吧,自己想想能用來做什么吧.
淘寶用它來做什么?
打開淘寶首頁,查看他源代碼,拖到最下面,內(nèi)容如下:

<!--city: fuzhou-->
<!--province: unknown-->
<!--hostname: -->
<!--hostname: home1.cn199-->

我們可以很清晰的看到,這邊有省和地區(qū)還有主機(jī)名,也就是淘寶真實(shí)服務(wù)器的主機(jī)名,處理我這個(gè)請(qǐng)求的主機(jī)名為home1.cn199, city取到了fuzhou,provinece省份沒取到,估計(jì)是它geo的問題
或者隨便打開一個(gè)商品頁面, 查看源代碼,如下:

</html>
<script type="text/javascript">tshop.initfoot({});</script>

可以看到他這邊給這頁面追加了一個(gè)js代碼,淘寶開發(fā)這個(gè)模塊的用意想必大家都明白了,集思廣益,或許大家還有更好的用處.
3. 怎么安裝nginx-http-footer-filter
3.1 下載地址:

https://github.com/alibaba/nginx-http-footer-filter/tree/1.2.2
3.2 安裝nginx-footer模塊
之前已經(jīng)安裝過nginx,所以我選擇覆蓋nginx文件。

# cd /usr/local/src/
# wget https://codeload.github.com/alibaba/nginx-http-footer-filter/zip/1.2.2
# unzip 1.2.2
 
# http://nginx.org/download/nginx-1.4.1.tar.gz
# tar -xzvf nginx-1.4.1.tar.gz
# cd nginx-1.4.1
# ./configure --prefix=/usr/local/nginx-1.4.1 \
--with-http_stub_status_module --with-http_realip_module \
--add-module=../nginx-http-footer-filter-1.2.2
# make
# mv /usr/local/nginx-1.4.1/sbin/nginx /usr/local/nginx-1.4.1/sbin/old_nginx
# mv objs/nginx /usr/local/nginx-1.4.1/sbin/
# /usr/local/nginx-1.4.1/sbin/nginx -s stop
# /usr/local/nginx-1.4.1/sbin/nginx

3.3 驗(yàn)證模塊是否安裝成功

# /usr/local/nginx-1.4.1/sbin/nginx -v
nginx version: nginx/1.4.1
built by gcc 4.4.7 20120313 (red hat 4.4.7-3) (gcc)
tls sni support enabled
configure arguments: --prefix=/usr/local/nginx-1.4.1 
--with-http_stub_status_module 
--with-http_realip_module 
--add-module=../nginx-http-footer-filter-1.2.2

4. 怎么使用nginx-http-footer-filter模塊
4.1 配置location
在location中使用footer "你的內(nèi)容" 即可.看如下配置

server {
    listen    173.255.219.122:80;
    server_name test.ttlsa.com;
    access_log /data/logs/nginx/test.ttlsa.com.access.log main;
 
    index index.html index.php index.html;
    root /data/site/test.ttlsa.com;
    location / {
      footer "<!-- $date_gmt -->";
      index index.html;
    }
 
    location =/html/2252.css {
      footer_types text/css;
      footer "/* host: $server_name - $date_local */";
}

4.2 測(cè)試nginx-footer效果

# cat 2252.shtml
<html>
  <head>
  <title>test</title>
  </head>
  <body>
    this is webpage
  </body>
</html>

訪問站點(diǎn)test.ttlsa.com/html/2252.shtml

Nginx服務(wù)器的nginx-http-footer-filter模塊怎么配置

如圖,我們可以看到文件最底部加上了<!-- 1376063369 -->,怎么變成了時(shí)間撮了,因?yàn)槲疫@邊是ssi的語法,如果你不知道什么是ssi,那么請(qǐng)參考文章什么是ssi.
[warning]他僅僅是在文件的最后一行追加,而不是<body>里面.這點(diǎn)大家要注意了.[/warning]
4.3 再來測(cè)試一下css文件

# cat 2242.css
# this is css file

如下是訪問結(jié)果:

# this is css file
/* host: test.ttlsa.com - 1376064324 */

看圖:

Nginx服務(wù)器的nginx-http-footer-filter模塊怎么配置

5. 我能寫多個(gè)footer指令嗎?
不行,以下我寫了兩個(gè)footer

location / {
  footer "12312321321";
  footer "<!-- $date_gmt -->";
  index index.html;
}

如下測(cè)試,提示footer指令重復(fù)了

# /usr/local/nginx-1.4.1/sbin/nginx -t
nginx: [emerg] "footer" directive is duplicate in /usr/local/nginx-1.4.1/conf/vhost/test.ttlsa.com.conf:13
nginx: configuration file /usr/local/nginx-1.4.1/conf/nginx.conf test failed

6. 只能用ssi變量嗎?
當(dāng)然不是,隨便你寫,可以是ssi指令,也可以是nginx變量,也可以是任何無意義的字符串
如下:

footer "12312321321";
footer "<!--12312321321-->";
footer "<!--$remote_addr-->";

比如我想知道這個(gè)頁面是哪臺(tái)web服務(wù)器處理的,那么我在底部插入主機(jī)名即可.這樣,有500錯(cuò)誤,我便可以馬上定位到具體的服務(wù)器了

footer "<!--$hostname-->";

返回結(jié)果如下:

Nginx服務(wù)器的nginx-http-footer-filter模塊怎么配置

7. 服務(wù)器返回500,404,403等錯(cuò)誤, 是否還會(huì)追加內(nèi)容到底部
會(huì),如果不追加,就無法通過返回的頁面得知哪臺(tái)web出現(xiàn)故障,這明顯就不符合作者的初衷了
配置如下:

location / {
  return 500;
  footer "<!--$hostname-->";
}

結(jié)果如下:

Nginx服務(wù)器的nginx-http-footer-filter模塊怎么配置

8. 模塊指令說明:
footer模塊非常簡(jiǎn)單,就只有兩個(gè)指令,具體說明如下
footer字符串
默認(rèn)值:
配置段: http, server, location
這個(gè)定義了將什么內(nèi)容追加到文件內(nèi)容的底部
footer_types mime類型
默認(rèn)值: footer_types: text/html
配置段: http, server, location

到此,相信大家對(duì)“Nginx服務(wù)器的nginx-http-footer-filter模塊怎么配置”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

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

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

AI