nginx向响应内容中追加内容(ngx_http_addition_module模块)
生活随笔
收集整理的這篇文章主要介紹了
nginx向响应内容中追加内容(ngx_http_addition_module模块)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
ngx_http_addition_module在響應之前或者之后追加文本內容,比如想在站點底部追加一個js或者css,可以使用這個模塊來實現,這個模塊和淘寶開發的nginx footer模塊有點類似,但是還是有不同. 這個模塊需要依賴子請求,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 如果你已經安裝了nginx,只想增加模塊,請參考ttlsa以前的文章 如何安裝nginx第三方模塊 2. 指令(Directives) 語法: ?? ?add_before_body uri; 默認值: ?? ?— 配置段: ?? ?http, server, location 發起一個子請求,請求給定的uri,并且將內容追加到主題響應的內容之前。 語法: ?? ?add_after_body uri; 默認值: ?? ?— 配置段: ?? ?http, server, location 發起一個子請求,請求給定的uri,并且將內容追加到主題響應的內容之后。 syntax: addition_types mime-type ...; default: addition_types text/html; context: http, server, location 這個指令在0.7.9開始支持,指定需要被追加內容的MIME類型,默認為“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 測試 以下三個文件,對應請求的主體文件和add_before_body、add_after_body對應的內容 # 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 訪問結果如下,可以看到20131001_add.html的頂部和底部分別嵌入了子請求header.html和footer的內容。 # 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. 結束語 addition模塊與上節上節nginx sub替換響應內容模塊應用場景有點相同,具體怎么使用,大家結合實際情況來使用.歡迎大家繼續訪問運維生存時間. 轉載請注明來自運維生存時間:http://www.ttlsa.com/html/3294.html轉載于:https://my.oschina.net/766/blog/210925
總結
以上是生活随笔為你收集整理的nginx向响应内容中追加内容(ngx_http_addition_module模块)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: memc_nginx+srcache_n
- 下一篇: 制作数据集-解析篇