nginx的with-http_sub_module模塊使用之替換字符串


一、介紹

該ngx_http_sub_module模塊是一個過濾器,通過將一個指定的字符串替換為另一個字符串來修改響應。該模塊不是默認生成的,它應該使用--with-http_sub_module 配置參數啟用 。

二、指令介紹

2.1、sub_filter指令

作用:用於替換字符串

用法:sub_filter   要替換的字符串   替換后的字符串,不區分字符串的大小寫

范圍:http   server   location

默認:-

2.2、sub_filter_last_modified指令

作用:允許在替換期間保留來自原始響應的“Last-Modified”標題字段以促進響應緩存。默認情況下,在處理期間修改響應的內容時,標題字段被刪除。

用法:sub_filter_last_modified  on | off

范圍:http   server   location

默認:sub_filter_last_modified  on

 2.3、sub_filter_once

作用:指示是否查找每個字符串以替換一次或重復替換。

用法:sub_filter_once  on | off

范圍:http   server   location

默認:http   server   location

2.4、sub_filter_types

作用:指定MIME類型的字符串替換,除了“ text/html” 之外,還可以在指定MIME類型的響應中啟用字符串替換。特殊值“ *”匹配任何MIME類型

用法:sub_filter_types mime-type ...

范圍:http   server   location

默認:sub_filter_types text / html;

三、操作演示

3.1、編譯安裝nginx

編譯安裝nginx,注意加上--with-http_sub_module模塊,這里提供編譯的參數:

./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module \
--with-http_sub_module \

 

3.2、安裝nginx

make && make  install

 

3.3修改配置文件nginx.conf

 location / {
           sub_filter 'nginx.com' 'baidu.com'; --把nginx.com替換成了baidu.com
           sub_filter 'nginx.org' 'buy.jiegeng.com';--把ngin.org替換成了buy.jiegeng.com
           sub_filter_types css/html;--替換的類型 sub_filter_once off;--全部替換
           root  html;
           index  index.html index.htm;
        }

3.4、重啟

/usr/sbin/nginx    -s   reload

3.5、驗證

3.5.1修改后頁面

 

3.5.2原始頁面

 四、支持正則的第三方模塊

使用ngx_http_sub_module模塊好處是nginx內置該模塊使用方便,不足之處在於該模塊不支持正則替換,靈活性不夠,

支持正則匹配替換的第三方模塊ngx_http_substitutions_filter_module:

4.1、下載

下載地址:https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip

 添加參數--add-module=/模塊解壓后文件路徑

./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module \
--with-http_sub_module \
--add-module=../ngx_http_substitutions_filter_module-master

4.2、subs_filter的使用介紹

* subs_filter
subs_filter 語法: subs_filter source_str destination_str [gior]
默認: none
適用: http,server,location
subs_filter 是用來替換文本的,可以使用正則
g(默認):替換匹配項。
i  :區分大小寫的匹配
o : 只匹配發現的第一個。
r  : 正則匹配。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM