Nginx反向代理和內(nèi)容替換模塊實現(xiàn)網(wǎng)頁內(nèi)容動態(tài)替換功能
Nginx是一款輕量級高性能服務(wù)器軟件,雖然輕量,但功能非常強(qiáng)大,可用于提供WEB服務(wù)、反向代理、負(fù)載均衡、緩存服務(wù)、甚至可以通過添加一些模塊搭建rtmp流媒體服務(wù)。最近碰到一個客戶需求,需要用到nginx網(wǎng)頁內(nèi)容替換模塊,貼出來跟大家交流,如有不足之處請指出。
ngx_http_sub_module模塊是一個過濾器,它修改網(wǎng)站響應(yīng)內(nèi)容中的字符串。這個模塊已經(jīng)內(nèi)置在nginx中,但是默認(rèn)未安裝,需要安裝需要加上配置參數(shù):--with-http_sub_module 如果已經(jīng)安裝nginx,只需要再添加這個模塊就可以了。
安裝替換
nginx官網(wǎng)下載安裝包:http://nginx.org/en/download.html
# wget http://nginx.org/download/nginx-1.11.5.tar.gz
# tar -zxvf nginx-1.11.5.tar.gz
# cd nginx-1.11.5
# ./configure --with-http_stub_status_module --with-http_sub_module && make && make install
常用指令
sub_filter指令
sub_filter string(原字符串) replacement(用于替換的字符串);
用于設(shè)置需要使用說明字符串替換說明字符串.string是要被替換的字符串,replacement是新的字符串,它里面可以帶變量。
sub_filter_last_modified指令
sub_filter_last_modified on | off;
用于設(shè)置網(wǎng)頁內(nèi)替換后是否修改 可在nginx.conf的 http, server, location三個位置配置使 用,默認(rèn)值是off;
sub_filter_once指令
sub_filter_once on | off;
用于設(shè)置字符串替換次數(shù),默認(rèn)只替換一次。如果是on,默認(rèn)只替換第一次匹配到的到字 符,如果是off,那么所有匹配到的字符都會被替換;
sub_filter_types指令
sub_filter_types *
用于指定需要被替換的MIME類型,默認(rèn)為“text/html”,如果制定為*,那么所有的;
說明:以上指令可在nginx.conf的http, server, location三個位置配置使用;
反向代理動態(tài)替換網(wǎng)頁內(nèi)容實例參考
server {
listen 80;
server_name mikecrm.xianzhixiong.com;
# 上傳文件大小限制
client_max_body_size 20M;
# 設(shè)置為on表示啟動高效傳輸文件的模式
sendfile on;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://mikecrm.com;
proxy_set_header Host mikecrm.com;
proxy_set_header Referer http://mikecrm.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
#proxy_request_buffering off;
proxy_set_header Accept-Encoding "";
sub_filter_types *;
sub_filter_once off;
#sub_filter_last_modified on;
sub_filter 'cdnq2.mikecrm.com' 'mikecrm.xianzhixiong.com';
sub_filter 'cdnq3.mikecrm.com' 'mikecrm.xianzhixiong.com';
sub_filter 'https://cdnq2plt.mikecrm.com' 'http://mikecrm.xianzhixiong.com';
sub_filter 'https://cdnq3plt.mikecrm.com' 'http://mikecrm.xianzhixiong.com';
sub_filter 'http://dlcn.mikecrm.com' '';
sub_filter 'http://mikecrm.com' '';
sub_filter 'https://mikecrm.com' '';
sub_filter 'www.mikecrm.com' 'mikecrm.xianzhixiong.com';
#sub_filter '.mikecrm.com' 'mikecrm.xianzhixiong.com';
#sub_filter 'mikecrm.com' 'mikecrm.xianzhixiong.com';
}
#location ~ \.php$ {
# proxy_pass https://real.mikecrm.com;
#}
#location ~ /plt.js$ {
# proxy_pass https://cdnq3plt.mikecrm.com;
#}
}參數(shù)解釋
注意只有在新版本nginx中才支持多sub_filter.
proxy_set_header Accept-Encoding "";
設(shè)置這個得原因是:告訴后端不要進(jìn)行g(shù)zip壓縮. 如果是gzip壓縮流, 那么我們就沒法進(jìn)行替換了.
sub_filter_types *;
對所有請求響應(yīng)類型都做sub_filter指定的替換.
sub_filter_once off;
sub_filter會執(zhí)行多次而不是一次. 效果類似于java中的string.replaceAll而不是replace.
sub_filter 'str1' 'str2';
替換字符串,str1是搜索的字符串,str2是最終被替換成的字符串
總結(jié)
到此這篇關(guān)于Nginx反向代理和內(nèi)容替換模塊實現(xiàn)網(wǎng)頁內(nèi)容動態(tài)替換功能的文章就介紹到這了,更多相關(guān)Nginx反向代理替換網(wǎng)頁字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx出現(xiàn)504 Gateway Time-out的解決方法
nginx訪問出現(xiàn)504 Gateway Time-out,一般是由于程序執(zhí)行時間過長導(dǎo)致響應(yīng)超時,本文就來介紹一下解決方法,感興趣的可以了解一下2023-10-10
詳解阿里云LINUX服務(wù)器配置HTTPS(NGINX)
本篇文章主要介紹了阿里云LINUX服務(wù)器配置HTTPS(NGINX) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
通過Nginx實現(xiàn)前端與后端的協(xié)同部署
在現(xiàn)代 web 開發(fā)中,前端與后端的協(xié)同部署是一個關(guān)鍵問題,一個高效的部署策略不僅能提升用戶體驗,還能簡化開發(fā)流程,今天,我們就來探討如何利用 Nginx 實現(xiàn)前端與后端的協(xié)同部署,需要的朋友可以參考下2025-03-03

