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

 更新时间:2018年03月30日 10:43:15   作者:飞鸿影  
本篇文章主要介绍了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

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

相关文章

  • 使用MongoDB分析Nginx日志的方法详解

    使用MongoDB分析Nginx日志的方法详解

    在项目开发过程中,总是离不开日志解析的工作,虽然有些时候觉得确实挺繁琐的,但是静下心来会发现有时候也是挺有趣的1件工作。 下面这篇文章主要介绍了使用MongoDB分析Nginx日志的方法,需要的朋友可以参考借鉴,一起来看看吧。
    2017-01-01
  • Nginx安装后/etc/nginx/conf.d下没有default.conf的解决

    Nginx安装后/etc/nginx/conf.d下没有default.conf的解决

    nginx.conf是nginx默认加载的配置文件 通过nginx -V可以看nginx默认配置文件路径,本文主要介绍了Nginx安装后/etc/nginx/conf.d下没有default.conf的解决,感兴趣的可以了解一下
    2023-11-11
  • Nginx源码安装的方法步骤

    Nginx源码安装的方法步骤

    这篇文章主要介绍了Nginx源码安装的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • nginx反向代理下的长连接实现

    nginx反向代理下的长连接实现

    本文主要介绍了Nginx反向代理下的长连接实现,包括客户端到Nginx和Nginx到后端服务器之间的长连接设置,具有一定的参考价值,感兴趣的可以了解一下
    2024-11-11
  • 在nginx中实现单位时间内限制访问频率的教程

    在nginx中实现单位时间内限制访问频率的教程

    这篇文章主要介绍了在nginx中实现单位时间内限制访问频率的教程,并非针对IP而是全局的访问量限制,需要的朋友可以参考下
    2015-04-04
  • nginx 499错误处理及nginx的配置参数小结

    nginx 499错误处理及nginx的配置参数小结

    在项目容器化改造中,修改Nginx超时设置可解决499错误,本文就来介绍一下nginx 499错误处理及nginx的配置参数小结,感兴趣的可以了解一下
    2024-09-09
  • windows下nginx的安装使用及解决80端口被占用nginx不能启动的问题

    windows下nginx的安装使用及解决80端口被占用nginx不能启动的问题

    这篇文章主要给大家介绍了关于windows下nginx的安装使用,以及如何解决80端口被占用导致nginx不能启动的问题,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-04-04
  • Nginx实现动静分离的示例代码

    Nginx实现动静分离的示例代码

    这篇文章主要介绍了Nginx实现动静分离的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • nginx如何指向本地路径及500错误解决方法

    nginx如何指向本地路径及500错误解决方法

    这篇文章主要为大家介绍了nginx如何指向本地路径及500错误解决方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Nginx进行域名重写和泛域名解析配置的方法

    Nginx进行域名重写和泛域名解析配置的方法

    这篇文章主要介绍了Nginx进行域名重写和泛域名解析配置的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08

最新评论