首頁  >  文章  >  後端開發  >  nginx使用replace-filter-nginx-module實現內容替換

nginx使用replace-filter-nginx-module實現內容替換

不言
不言原創
2018-03-30 13:40:192561瀏覽

本文所給大家分享的是nginx使用replace-filter-nginx-module實現內容替換,很詳細,有需要的朋友可以參考一下

有時候我們想對回應(例如PHP介面)傳回的內容做些字串,雖然可以使用各語言程式碼相關方法(例如PHP的str_replace)進行替換,但在nginx層級替換是更方便的,無需修改程式碼。

約定:本文原始碼目錄統一放在:/root/soft/src 裡。

安裝

安裝此模組需要先安裝sregex 執行庫:

$ git clone https://github.com/agentzh/sregex
$ cd sregex
$ make
$ make install

然後安裝replace-filter-nginx-module 模組:

$ cd /root/soft/src
$ git clone https://github.com/agentzh/replace-filter-nginx-module
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ 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

參考

1、已安裝nginx動態新增模組
#http://coolnull.com/4245.html
2、LNMP平滑升級nginx並安裝ngx_lua模組教學
http://www.mamicode.com/info-.. .
3、openresty/replace-filter-nginx-module: Streaming regular expression replacement in response bodies
https://github.com/openresty/...
4、比ngx_http_substitutions_filter_module 更強大的替換模組sregex的replace-filter-nginx-module  
https://www.cnblogs.com/archo...

原載於:http://www.cnblogs.com/52fhy/...

相關推薦:

nginx如何修改上傳檔案大小

nginx設定PHP實例

PHP + Nginx環境下curl存取本機逾時


#

以上是nginx使用replace-filter-nginx-module實現內容替換的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn