nginx使用replace-filter-nginx-module實現內容替換的示例
有時候我們想對響應(例如PHP介面)返回的內容做些字串,雖然可以使用各語言程式碼相關方法(例如PHP的str_replace)進行替換,但是在nginx層面替換是更方便的,無需修改程式碼。
約定:本文原始碼目錄統一放在:/root/soft/src 裡。
安裝
安裝此模組需要先安裝 sregex 執行庫:
$ git clone
$ cd sregex
$ make
$ make install
然後安裝 replace-filter-nginx-module 模組:
$ cd /root/soft/src
$ git clone
$ wget
$ tar zxvf nginx-1.12.2.tar.gz
這裡採用靜態編譯的方式安裝模組,需要重新編譯 nginx 。獲取 nginx 上一次編譯引數:
$ nginx -V
nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-ld-opt=-ljemalloc --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module
這裡新增 replace-filter-nginx-module 模組:
--add-module=/root/soft/src/replace-filter-nginx-module
最終編譯命令:
cd nginx-1.12.2
$ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-ld-opt=-ljemalloc --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --add-module=/root/soft/src/replace-filter-nginx-module
$ make
由於這裡是升級nginx,所以千萬不要make install,不然就真的覆蓋了。 接下來手動替換二進位制檔案:
$ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
$ cp -rfp ./objs/nginx /usr/local/nginx/sbin/
測試是否可行:
$ nginx -v
nginx version: nginx/1.12.2
配置是否正常:
$ /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
配置
這裡測試的是:把介面返回的 img.test.com 換成 media.test.com。
修改: /usr/local/nginx/conf/vhost/test.com.conf
location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
為:
location ~ [^/]\.php(/|$)
{
replace_filter 'img.test.com' 'media.test.com' g;
replace_filter_types application/json;
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
注意需要新增replace_filter_types 。
儲存後檢查配置是否ok:
$ /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
然後熱重啟:
# 升級完成第一次需要重啟
$ service nginx restart
# 熱重啟
$ /usr/local/nginx/sbin/nginx -s reload
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69948194/viewspace-2668928/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- nginx使用replace-filter-nginx-module實現內容替換NginxFilter
- Nginx實現對響應體內容的替換Nginx
- jQuery實現的將指定元素中的內容替換jQuery
- js實現的替換字串中的全部指定內容JS字串
- 如何在MySQL中實現替換欄位部分內容MySql
- vim內替換檔案內容
- js實現的對字串中的指定內容進行替換操作JS字串
- linux替換文字內容Linux
- JavaScript 替換字串全部指定內容JavaScript字串
- javascript如何替換字串中的指定內容JavaScript字串
- js替換字串中的所有指定內容JS字串
- 替換文字檔案中指定的內容
- 使用SQL語言 替換資料庫某欄位內的部分內容SQL資料庫
- 批次word文件內容查詢替換的方法
- PHP 實現自動新增或者替換 內容的IMG標籤的 alt title 屬性PHP
- handlebars.js 用 <br>替換掉 內容的換行符JS
- grep、sed批量替換檔案內容shell
- SQL Server資料庫內容替換方法SQLServer資料庫
- idea替換內容快捷鍵 idea怎麼替換掉所選的文字Idea
- replace()方法替換字串內容程式碼例項字串
- linux下批次替換檔案內容(摘)Linux
- JS和C#實現的兩個正則替換功能示例分析JSC#
- php文章內容替換為內鏈,有助於SEO優化PHP優化
- Sql Server 替換某一列中指定的文字內容SQLServer
- mysql修改某個欄位(替換關鍵字內容)MySql
- 實現最簡單的模板替換
- python 檔案操作(二) 替換性修改檔案內容Python
- 如何實現pre標籤中的內容自動換行
- 行內元素與塊狀元素 行內替換元素與行內非替換元素的區別
- 教你如何用 openresty 完美替換 nginxRESTNginx
- WinForm使用DataGridView實現類似Excel表格的查詢替換ORMViewExcel
- Android:使用SpannableString實現圖片替換相應的文字Android
- QT之——QTableWidget拖拽單元格並替換內容(進階)QT
- mysql 如何替換資料表欄位字串中指定單詞的內容MySql字串
- linux批量替換指定資料夾中所有檔案的指定內容Linux
- 使用 sed 命令查詢和替換檔案中的字串的 16 個示例字串
- winget 替換國內源
- Linux給檔案內容每行指定字元資料脫敏替換Linux字元