nginx向响应内容中追加内容(ngx_http_addition_module模块)
生活随笔
收集整理的這篇文章主要介紹了
nginx向响应内容中追加内容(ngx_http_addition_module模块)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
ngx_http_addition_module在響應(yīng)之前或者之后追加文本內(nèi)容,比如想在站點(diǎn)底部追加一個(gè)js或者css,可以使用這個(gè)模塊來實(shí)現(xiàn),這個(gè)模塊和淘寶開發(fā)的nginx footer模塊有點(diǎn)類似,但是還是有不同. 這個(gè)模塊需要依賴子請求,nginx footer依賴nginx寫死的配置. 1. 安裝nginx # wget http://nginx.org/download/nginx-1.4.2.tar.gz # tar -xzvf nginx-1.4.2.tar.gz # cd nginx-1.4.2 #? --prefix=/usr/local/nginx-1.4.2 --with-http_stub_status_module --with-http_addition_module # make # make install 如果你已經(jīng)安裝了nginx,只想增加模塊,請參考ttlsa以前的文章 如何安裝nginx第三方模塊 2. 指令(Directives) 語法: ?? ?add_before_body uri; 默認(rèn)值: ?? ?— 配置段: ?? ?http, server, location 發(fā)起一個(gè)子請求,請求給定的uri,并且將內(nèi)容追加到主題響應(yīng)的內(nèi)容之前。 語法: ?? ?add_after_body uri; 默認(rèn)值: ?? ?— 配置段: ?? ?http, server, location 發(fā)起一個(gè)子請求,請求給定的uri,并且將內(nèi)容追加到主題響應(yīng)的內(nèi)容之后。 syntax: addition_types mime-type ...; default: addition_types text/html; context: http, server, location 這個(gè)指令在0.7.9開始支持,指定需要被追加內(nèi)容的MIME類型,默認(rèn)為“text/html”,如果制定為*,那么所有的 3. nginx配置addition 3.1 配置nginx.conf server {listen?????? 80;server_name? www.ttlsa.com;root /data/site/www.ttlsa.com;?? ?location / {add_before_body /2013/10/header.html;add_after_body? /2013/10/footer.html;} } 3.2 測試 以下三個(gè)文件,對應(yīng)請求的主體文件和add_before_body、add_after_body對應(yīng)的內(nèi)容 # cat /data/site/test.ttlsa.com/2013/10/20131001_add.html <html> <head> <title>I am title</title> </head> <body> ngx_http_addition_module </body> </html># cat /data/site/test.ttlsa.com/2013/10/header.html I am header!# cat /data/site/test.ttlsa.com/2013/10/footer.html footer - ttlsa 訪問結(jié)果如下,可以看到20131001_add.html的頂部和底部分別嵌入了子請求header.html和footer的內(nèi)容。 # curl test.ttlsa.com/2013/10/20131001_add.html????????? ? I am header! <html> <head> <title>I am title</title> </head> <body> ngx_http_addition_module </body> </html> footer - ttlsa 4. 結(jié)束語 addition模塊與上節(jié)上節(jié)nginx sub替換響應(yīng)內(nèi)容模塊應(yīng)用場景有點(diǎn)相同,具體怎么使用,大家結(jié)合實(shí)際情況來使用.歡迎大家繼續(xù)訪問運(yùn)維生存時(shí)間. 轉(zhuǎn)載請注明來自運(yùn)維生存時(shí)間:http://www.ttlsa.com/html/3294.html轉(zhuǎn)載于:https://my.oschina.net/766/blog/210925
總結(jié)
以上是生活随笔為你收集整理的nginx向响应内容中追加内容(ngx_http_addition_module模块)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: memc_nginx+srcache_n
- 下一篇: PHP代码调试神器Whoops