小知识:nginx使用replace-filter-nginx-module实现内容替换的示例

有时候我们想对响应(例如PHP接口)返回的内容做些字符串,虽然可以使用各语言代码相关方法(例如PHP的str_replace)进行替换,但是在nginx层面替换是更方便的,无需修改代码。

约定:本文源代码目录统一放在:/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

这里采用静态编译的方式安装模块,需要重新编译 nginx 。获取 nginx 上一次编译参数:

?
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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

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

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:17个Linux tar命令实用示例

2023-4-19 2:45:48

建站知识

小知识:Linux 下目录文件权限(命令)的查看和修改

2023-4-19 2:54:04

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索