nginx使用replace-filter-nginx-module实现内容替换的示例
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,有时候我们想对响应(例如PHP接口)返回的内容做些字符串,虽然可以使用各语言代码相关方法(例如PHP的str_replace)进行替换,但是在nginx层面替换是更方便的,无需修改代码。约定:本文源代
千家信息网最后更新 2025年12月01日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;注意需要添加replace_filter_types 。
保存后检查配置是否ok:
$ /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
然后热重启:
# 升级完成第一次需要重启$ service nginx restart # 热重启$ /usr/local/nginx/sbin/nginx -s reload
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
模块
编译
配置
内容
代码
接口
升级
测试
可行
接下来
二进制
参数
命令
字符
字符串
就是
层面
手动
文件
方式
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
地下城与勇士服务器有什么区别
网络技术研究生毕业生工资
北京浪潮云服务器
雷达数据库
数据库rac数据库服务
学校关于网络安全承诺书
数据库创建后怎么保存
怎么查看附加数据库的代码
网络安全安全的手抄报
atere是数据库的语句吗
网络安全问题个人情况说明
达梦数据库 列存
骑士战争服务器
网络安全检测怎么删除
软件开发的自我分析
培训软件开发的学校
国家网络安全宣传周活动主场
软件开发自学课程教学视频
多人语音交友软件开发定制
沃尔玛图形数据库应用
卫星应用基础支撑数据库
泉州市网络安全平台招标
服务器数据删除恢复
软件开发用哪些工具
云服务器品牌
戴尔架式服务器如何揭盖
网络安全问题会导致文件丢失吗
重庆矗鑫互联网科技
培训软件开发的学校
连接别人的数据库能建表空间吗