资讯专栏INFORMATION COLUMN

nginx使用replace-filter-nginx-module实现内容替换

0xE7A38A / 3517人阅读

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

有时候我们想对响应(例如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
参考

1、已安装nginx动态添加模块
http://coolnull.com/4245.html
2、LNMP平滑升级nginx并安装ngx_lua模块教程
http://www.mamicode.com/info-...
3、openresty/replace-filter-nginx-module: Streaming regular expression replacement in response bodies
https://github.com/openresty/...
4、比ngx_http_substitutions_filter_module 更强大的替换模块sregex的replace-filter-nginx-module
https://www.cnblogs.com/archo...

原载于:http://www.cnblogs.com/52fhy/...

欢迎关注公众号及时获取最新文章推送!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39869.html

相关文章

  • Nginx基础篇(3)- Nginx常用官方模块

    摘要:常用官方模块采用模块化的架构,中大部分功能都是通过模块方式提供的,比如模块模块等。一般是在开启长连接情况下出现。 Nginx常用官方模块 Nginx采用模块化的架构,Nginx中大部分功能都是通过模块方式提供的,比如HTTP模块、Mail模块等。 Nginx官方模块文档 1. ngx_http_stub_status_module 编译选项 --with-http_stub_sta...

    Drummor 评论0 收藏0
  • Nginx基础篇(3)- Nginx常用官方模块

    摘要:常用官方模块采用模块化的架构,中大部分功能都是通过模块方式提供的,比如模块模块等。一般是在开启长连接情况下出现。 Nginx常用官方模块 Nginx采用模块化的架构,Nginx中大部分功能都是通过模块方式提供的,比如HTTP模块、Mail模块等。 Nginx官方模块文档 1. ngx_http_stub_status_module 编译选项 --with-http_stub_sta...

    blankyao 评论0 收藏0
  • Nginx基础笔记

    摘要:压力测试工具请求数并发数请求是一个高性能的和反向代理服务,也是一个服务。 压力测试工具:ab ab -n 请求数 -c 并发数 请求url Nginx: Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。 特点: IO多路复用epoll 轻量级 CPU亲和(affinity):把每个worker进程固定在一个cpu上执...

    caige 评论0 收藏0
  • Nginx深入了解-基础(三)

    nginx有很多模块配置,包括官方模块和第三方模块。 http_stub_status_modules配置 Syntax:stub_statusDefault:--Context:server,location 例如: server { ... location /mystatus { stub_status; } } random_index_modul...

    android_c 评论0 收藏0

发表评论

0条评论

0xE7A38A

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<