nginx使用replace-filter-nginx-module实现内容替换的示例
时间:2019-04-13
本文章向大家介绍nginx使用replace-filter-nginx-module实现内容替换的示例,主要包括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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- IP不能作为rocketmq 的topic
- Unity3D网络通讯(六)-- UnityWebRequest实现WebService通讯
- Java Maven编译时没问题,运行时报java.lang.NoSuchMethodError
- 树莓派基础实验34:L298N模块驱动直流电机实验
- The consumer group[PushConsumer] has been created before, specify another name please
- 虚拟机系列 | JVM特点,基础结构与执行周期
- 报错 解决-bash: fork: retry: Resource temporarily unavailable
- 结构与算法(04):排序规则与查找算法
- 手把手教你在 TKE 集群中实现简单的蓝绿发布和灰度发布
- 虚拟机系列 | JVM类加载机制
- 树莓派综合项目2:智能小车(一)四轮驱动
- java 字节流入门(内存数组流->文件流)
- 视频高速上云网关/网络穿透EasyNTS智能组网服务平台ini配置文件丢失如何处理?
- 结构与算法(05):二叉树与多叉树
- 树莓派综合项目2:智能小车(二)tkinter图形界面控制