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

约定:本文源代码目录统一放在:/root/soft/src 里。

开始安装

安装sregex运行库

安装此模块需要先安装 sregex 运行库:

$ git clone https://github.com/agentzh/sregex
$ cd sregex
$ make
$ make install

软链接:

$ ln -sv /usr/local/lib/libsregex.so.0.0.1 /lib64/libsregex.so.0

安装 replace-filter-nginx-module

然后获取 replace-filter-nginx-module 模块,并解压,无需安装:

$ cd /root/soft/src
$ git clone https://github.com/agentzh/replace-filter-nginx-module

获取Nginx源码:

$ 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-0.01rc5

$ 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

编译动态模块

replace-filter-nginx-module 支持以动态模块方式加载,详见:https://github.com/openresty/replace-filter-nginx-module#installation 。Nginx版本需要 >=1.9.11

$  ./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-dynamic-module=/root/soft/src/replace-filter-nginx-module $ make -j2
$ make install

相比静态编译,参数--add-module改成了--add-dynamic-module

编译成功后,会把模块安装在nginx/modules/目录。查看:

$ ls /usr/local/nginx/modules/
ngx_http_replace_filter_module.so

接下来我们需要在nginx.conf配置中加入以下内容,实现动态调用模块:

load_module /usr/local/nginx/modules/ngx_http_replace_filter_module.so;

注意:load_module指令不能放在 http{} 里面:

worker_processes  1;

load_module xxx;

#error_log  logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http { }

接下来可以按上面的 测试 小节测试。唯一不同的是无需使用service nginx restart重启Nginx,直接使用nginx -s reload热重启就行了。

参考

1、已安装nginx动态添加模块

http://coolnull.com/4245.html

2、LNMP平滑升级nginx并安装ngx_lua模块教程

http://www.mamicode.com/info-detail-1110824.html

3、openresty/replace-filter-nginx-module: Streaming regular expression replacement in response bodies

https://github.com/openresty/replace-filter-nginx-module#synopsis

4、比ngx_http_substitutions_filter_module 更强大的替换模块sregex的replace-filter-nginx-module

https://www.cnblogs.com/archoncap/p/4973009.html

5、Nginx编译安装Lua - 飞鸿影~ - 博客园

https://www.cnblogs.com/52fhy/p/10164553.html

nginx使用replace-filter-nginx-module实现内容替换的更多相关文章

  1. 内容替换Filter

    有时候需要对网站进行控制,防止输出非法内容或者敏感信息.这时我们可以使用filter来进行内容替换,其工作原理为,在Servlet将内容输出到response时,response将内容缓存起来,在Fi ...

  2. nginx上传模块—nginx upload module-

    一. nginx upload module原理 官方文档: http://www.grid.net.ru/nginx/upload.en.html Nginx upload module通过ngin ...

  3. 使用Nginx反向代理和内容替换模块实现网页内容动态替换功能

    2016年11月21日 10:30:00 xian_02 阅读数:10943   Nginx是一款轻量级高性能服务器软件,虽然轻量,但功能非常强大,可用于提供WEB服务.反向代理.负载均衡.缓存服务. ...

  4. This module embeds Lua, via LuaJIT 2.0/2.1, into Nginx and by leveraging Nginx's subrequests, allows the integration of the powerful Lua threads (Lua coroutines) into the Nginx event model.

    openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers https://github.com/openre ...

  5. Nginx:无处不在的Nginx的八个应用场景与配置

    --- 阅读时间约 15 分钟 --- Nginx概述 众所周知,互联网已经离不开  WEB服务器  ,技术领域中  WEB服务器  不止  Nginx  一个,其他还有很多如  Apache  . ...

  6. paip.输出内容替换在Apache 过滤器filter的设置

    paip.输出内容替换在Apache 过滤器filter的设置 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...

  7. nginx(五)nginx与php的安装配置

    经过前面学习,对nginx有个大概的了解,来配置LNMP;只要是在系统安装过程中选择安装比较齐全的包,基本上系统都能满足安装要求,下面是我一个一个测试的,基本上全部安装所需的库文件,放心安装: [ro ...

  8. [nginx] 网上最全面nginx教程(近100篇文章整理)

    转载:http://bbs.linuxtone.org/thread-25588-1-1.html Nginx基础 1.  nginx安装 2.  nginx 编译参数详解 3.  nginx安装配置 ...

  9. Nginx系列2之Nginx+php

    preface 公司所有的大多数业务都泡在LNMP平台上,所以对PHP+Nginx有点了解,那么就做个小小的总结吧. what's FastCGi FastCGI是一个可伸缩,高速的在HTTP ser ...

随机推荐

  1. servlet过滤器简化版

    什么是过滤器 在struts2 中集成了过滤器,并可以根据需要选择合适自己的过滤器进行配置 , 过滤器:是基于函数回调的,运用java中的反射机制工作在struts2只能对于action起作用,在se ...

  2. JavaScript面向对象深入理解原型

    原型模式 function Person(){ } Person.prototype.name="Ewarm"; Person.prototype.age="29&quo ...

  3. Linux系列教程(四)——Linux文件和目录处理命令

    这个系列教程的前面我们讲解了如何安装Linux系统,以及学习Linux系统的一些方法.那么从这篇博客开始,我们就正式进入Linux命令的学习.学习命令,首先要跟大家纠正的一点就是,我们不需要记住每一条 ...

  4. 注解的形式与xml文件的形式完成事务管理及xml文件的配置

    需要的jar包: c3p0-0.9.2.1.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1 ...

  5. velocity的基础使用

    velocity的基本使用要求:掌握jsp的jstl技术,因为velocity的用法和jstl非常相似.语法上差别不大,但是velocity的示例明显比jstl少,解释也少,所以使用velocity必 ...

  6. 机器学习之三:logistic回归(最优化)

    一般来说,回归不用在分类问题上,因为回归是连续型模型,而且受噪声影响比较大.如果非要应用进入,可以使用logistic回归. logistic回归本质上是线性回归,只是在特征到结果的映射中加入了一层函 ...

  7. Windows环境下多线程编程原理与应用读书笔记(4)————线程间通信概述

    <一>线程间通信方法 全局变量方式:进程中的线程共享全局变量,可以通过全局变量进行线程间通信. 参数传递法:主线程创建子线程并让子线程为其服务,因此主线程和其他线程可以通过参数传递进行通信 ...

  8. sql语句练习题及答案

    表结构 创建表数据    SET NAMES utf8;    SET FOREIGN_KEY_CHECKS = 0;    -- ----------------------------    -- ...

  9. 一条咸鱼的梦--python

    毕业到现在已经有一年多了,或者说已经工作了一年多,这样以一个社会人的说法比较贴切吧.工作的这段时间里,我曾经有无数次的在问我该干什么,我想干什么,这好像一个深奥的哲学问题,好像并不是只有我一个毕业生思 ...

  10. Hadoop,master和slave简单的分布式搭建

    搭建过程中配置免密钥登录为了以后方便使用 [提醒]安装Hadoop中会遇到新建文件夹,配置路径等问题,这个不能生搬硬套,要使用自己配置的路径,灵活使用. Hadoop的部署配置文件在http://bl ...