国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

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

2024-08-30 12:23:11
字體:
來源:轉載
供稿:網友
本篇文章主要介紹了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 -Vnginx version: nginx/1.12.2built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure 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 -vnginx version: nginx/1.12.2

配置是否正常:

$ /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: 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;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建德市| 岱山县| 股票| 盈江县| 长兴县| 古田县| 德格县| 甘孜县| 桑日县| 酒泉市| 高阳县| 长岛县| 霍邱县| 德阳市| 西乌珠穆沁旗| 屏南县| 衡东县| 六安市| 清远市| 普兰县| 普定县| 贵阳市| 湖北省| 平乐县| 泸西县| 河津市| 昌宁县| 剑川县| 子洲县| 威宁| 黑龙江省| 禄丰县| 新密市| 岱山县| 苗栗市| 涟源市| 漯河市| 红河县| 建始县| 永城市| 临海市|