激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

云服務(wù)器|WEB服務(wù)器|FTP服務(wù)器|郵件服務(wù)器|虛擬主機|服務(wù)器安全|DNS服務(wù)器|服務(wù)器知識|Nginx|IIS|Tomcat|

服務(wù)器之家 - 服務(wù)器技術(shù) - Nginx - nginx使用replace-filter-nginx-module實現(xiàn)內(nèi)容替換的示例

nginx使用replace-filter-nginx-module實現(xiàn)內(nèi)容替換的示例

2019-12-16 13:54飛鴻影 Nginx

本篇文章主要介紹了nginx使用replace-filter-nginx-module實現(xiàn)內(nèi)容替換的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

有時候我們想對響應(yīng)(例如PHP接口)返回的內(nèi)容做些字符串,雖然可以使用各語言代碼相關(guān)方法(例如PHP的str_replace)進行替換,但是在nginx層面替換是更方便的,無需修改代碼。

約定:本文源代碼目錄統(tǒng)一放在:/root/soft/src 里。

安裝

安裝此模塊需要先安裝 sregex 運行庫:

?
1
2
3
4
$ git clone https://github.com/agentzh/sregex
$ cd sregex
$ make
$ make install

然后安裝 replace-filter-nginx-module 模塊:

?
1
2
3
4
$ 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

這里采用靜態(tài)編譯的方式安裝模塊,需要重新編譯 nginx 。獲取 nginx 上一次編譯參數(shù):

?
1
2
3
4
5
6
$ 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 模塊:

?
1
--add-module=/root/soft/src/replace-filter-nginx-module

最終編譯命令:

?
1
2
3
4
5
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,不然就真的覆蓋了。 接下來手動替換二進制文件:

?
1
2
$ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
$ cp -rfp ./objs/nginx /usr/local/nginx/sbin/

測試是否可行:

?
1
2
$ nginx -v
nginx version: nginx/1.12.2

配置是否正常:

?
1
2
3
$ /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

?
1
2
3
4
5
6
7
8
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;
}

為:

?
1
2
3
4
5
6
7
8
9
10
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:

?
1
2
3
$ /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

然后熱重啟:

?
1
2
3
4
5
# 升級完成第一次需要重啟
$ service nginx restart
 
# 熱重啟
$ /usr/local/nginx/sbin/nginx -s reload

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://segmentfault.com/a/1190000014077313

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久9久久 | 中国一级免费视频 | 亚洲国产高清视频 | 久久免费观看一级毛片 | 久久久久久久.comav | 欧美成人免费香蕉 | 久久久日韩精品一区二区三区 | 国产精品久久久久久久午夜片 | 韩日黄色片 | 亚洲成年人免费网站 | 天天碰天天操 | 99ri精品| 91精品国产91 | 黄网站色成年大片免费高 | 亚洲第一成网站 | 12av毛片| 国产一国产一级毛片视频在线 | 亚洲一区在线免费视频 | 久久精品久 | 欧美爱爱视频免费看 | 线观看免费完整aaa 久久不雅视频 | 国产成年人在线观看 | 中文字幕在线永久 | 久久精品视频在线免费观看 | 国产69精品99久久久久久宅男 | 狠狠干最新网址 | 国产91一区 | 久久精品女人天堂av | 线观看免费完整aaa 一二区成人影院电影网 | 国产高潮好爽好大受不了了 | 亚洲精品久久久久久 | 亚洲国产精品一区二区久久 | 午夜激情视频网站 | 西川av在线一区二区三区 | 国产免费看片 | 久久久久久久免费看 | 一级黄色免费观看视频 | 亚洲小视频在线 | 久久成人免费网 | 92看片淫黄大片欧美看国产片 | jizzjizz中国少妇中文 |