nginx模塊nginx-http-footer-filter(淘寶開發)

      nginx-http-footer-filter想必你們都以爲很陌生,那咱們就來認識一下它吧,這是淘寶開發的nginx模塊. 它用於nginx在響應請求文件底部追加內容. 今天抽空研究下這個插件,但願對你們有所幫助。爲何發現了這個插件,由於這幾天公司須要在全部shtml文件後面追加一個js代碼用來作統計(以前統計 代碼沒加齊全),在尋求解決方法的過程當中找到了它認識了它最後喜歡上了它,你可能覺得我用這個插件去實現了我要的功能,其實在認識他以前我用shell腳 本替換齊全了. 不過我仍是決定研究測試一下nginx-http-footer-filter,或許之後的需求上能有幫助,更或許能幫上其餘須要幫助的人.進入正題吧. javascript

1. nginx-http-footer-filter究竟是作什麼的?

說白了,就是在請求的頁面底部插入你要插入的代碼。 php

2. 咱們能用nginx-http-footer-filter來作什麼?

一、統一追加js代碼用於統計(我是這麼想的)
二、底部追加響應這個請求的realsver(後端真實服務器)信息,便於系統管理員排查故障.
三、你管理着數量龐大的虛擬主機,在全部web後面追加你的廣告代碼,黑鏈什麼的(很無恥)
四、觸類旁通吧,本身想一想能用來作什麼吧. css

淘寶用它來作什麼? html

打開淘寶首頁,查看他源代碼,拖到最下面,內容以下: java

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

咱們能夠很清晰的看到,這邊有省和地區還有主機名,也就是淘寶真實服務器的主機名,處理我這個請求的主機名爲home1.cn199, city取到了fuzhou,provinece省份沒取到,估計是它Geo的問題
或者隨便打開一個商品頁面, 查看源代碼,以下: nginx

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

能夠看到他這邊給這頁面追加了一個js代碼,淘寶開發這個模塊的用意想必你們都明白了,集思廣益,或許你們還有更好的用處. git

3. 怎麼安nginx-http-footer-filter

3.1 下載地址:

https://github.com/alibaba/nginx-http-footer-filter/tree/1.2.2


3.2 安裝nginx-footer模塊

以前已經安裝過nginx,因此我選擇覆蓋nginx文件。 github

# 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 驗證模塊是否安裝成功

# /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 "你的內容" 便可.看以下配置 web

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 測試nginx-footer效果

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

訪問站點test.ttlsa.com/html/2252.shtml shell

nginx追加底部代碼nginx追加底部代碼

如圖,咱們能夠看到文件最底部加上了<!-- 1376063369 -->,怎麼變成了時間撮了,由於我這邊是ssi的語法,若是你不知道什麼是ssi,那麼請參考文章什麼是ssi.

他僅僅是在文件的最後一行追加,而不是<body>裏面.這點你們要注意了.

4.3 再來測試一下css文件

# cat 2242.css
# this is css file

以下是訪問結果:
# this is css file
/* host: test.ttlsa.com - 1376064324 */

看圖:

nginx footer追加底部代碼cssnginx footer追加底部代碼css

5. 我能寫多個footer指令嗎?

不行,如下我寫了兩個footer

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

以下測試,提示footer指令重複了

# /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變量嗎?

固然不是,隨便你寫,能夠是ssi指令,也能夠是nginx變量,也能夠是任何無心義的字符串
以下:
footer "12312321321";
footer "<!--12312321321-->";
footer "<!--$remote_addr-->";

好比我想知道這個頁面是哪臺web服務器處理的,那麼我在底部插入主機名便可.這樣,有500錯誤,我即可以立刻定位到具體的服務器了

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

返回結果以下:

nginx footer 追加底部代碼nginx變量hostnamenginx footer 追加底部代碼nginx變量hostname

7. 服務器返回500,404,403等錯誤, 是否還會追加內容到底部

會,若是不追加,就沒法經過返回的頁面得知哪臺web出現故障,這明顯就不符合做者的初衷了,有些同窗可能以爲我在後端設置一個header不就能夠了,爲何還要去裝一個插件,麻煩不麻煩?不信你試試404,500這些錯誤頭部能不能看到。何況你面對的用戶都是小白們,難道讓他提交錯誤的時候,問他你看下你的http頭部的某值是多少,別人傻眼了。如今你只要說,哥們,網頁保存下來傳給我。

配置以下:

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

結果以下:

nginx footer追加底部代碼500錯誤nginx footer追加底部代碼500錯誤

8. 模塊指令說明:

footer模塊很是簡單,就只有兩個指令,具體說明以下
footer字符串
默認值: ``
配置段: http, server, location
這個定義了將什麼內容追加到文件內容的底部

footer_types MIME類型
默認值: footer_types: text/html
配置段: http, server, location
定義被追加底部文件的MIME返回類型,默認值是text/html

 我原文: nginx模塊nginx-http-footer-filter

連接失效點我:http://www.ttlsa.com/html/2252.html

相關文章
相關標籤/搜索