目錄
安裝
設定
參考
首頁 後端開發 php教程 nginx使用replace-filter-nginx-module實現內容替換

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

Mar 30, 2018 pm 01:40 PM
nginx 內容

本文所給大家分享的是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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

tomcat伺服器怎麼能讓外網訪問 tomcat伺服器怎麼能讓外網訪問 Apr 21, 2024 am 07:22 AM

tomcat伺服器怎麼能讓外網訪問

nginx啟動指令和停止指令是什麼 nginx啟動指令和停止指令是什麼 Apr 02, 2024 pm 08:45 PM

nginx啟動指令和停止指令是什麼

thinkphp怎麼運行 thinkphp怎麼運行 Apr 09, 2024 pm 05:39 PM

thinkphp怎麼運行

Welcome to nginx!怎麼解決? Welcome to nginx!怎麼解決? Apr 17, 2024 am 05:12 AM

Welcome to nginx!怎麼解決?

nodejs專案怎麼部署到伺服器 nodejs專案怎麼部署到伺服器 Apr 21, 2024 am 04:40 AM

nodejs專案怎麼部署到伺服器

phpmyadmin怎麼註冊 phpmyadmin怎麼註冊 Apr 07, 2024 pm 02:45 PM

phpmyadmin怎麼註冊

造訪網站出現nginx怎麼解決 造訪網站出現nginx怎麼解決 Apr 02, 2024 pm 08:39 PM

造訪網站出現nginx怎麼解決

docker容器之間如何通信 docker容器之間如何通信 Apr 07, 2024 pm 06:24 PM

docker容器之間如何通信

See all articles