利用Nginx sub模塊對網頁內容進行替換

在我們環境下,經常需要運營對網站進行測試,但是偶爾會出現不知道是在測試環境還是正式環境,因爲測試環境都是通過Nginx反向代理進行訪問.因此本文利用Nginx進行網頁內容替換,然後在醒目位置進行提醒,以達到對測試環境的標識作用


在編譯nginx的時候添加--with-http_sub_module以加載sub模塊

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module

如果全是測試環境的反向代理就只需要在http裏頭添加,當然也可以針對單個站點

sub_filter <body '<div style="border: red 1px solid;width: 100px;text-align: center;line-height: 30px;height: 30px;font-size: 14px;z-index: 999999999;position: fixed;top: 80px;font-weight: bold;background-color: yellow;left: 10px;">測試服務器</div><body';
sub_filter_once off;
sub_filter_types application/json application/x-javascript text/plain;

上面就是將"<body"替換爲"<div style="border: red 1px solid;width: 100px;text-align: center;line-height: 30px;height: 30px;font-size: 14px;z-index: 999999999;position: fixed;top: 80px;font-weight: bold;background-color: yellow;left: 10px;">測試服務器</div><body"

格式就是

sub_filter src_string  dst_string;

sub_filter_once off;

sub_filter_types application/json application/x-javascript text/plain;


然後效果就是

wKioL1eEpJ-TOXZ0AAAmSXTREIk013.png

每打開一次頁面都會有這個提示,就很容易知道是測試服務器了.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章