有时候咱们想对响应(例如PHP接口)返回的内容作些字符串,虽然可使用各语言代码相关方法(例如PHP的
str_replace
)进行替换,可是在nginx层面替换是更方便的,无需修改代码。php
约定:本文源代码目录统一放在:/root/soft/src
里。html
安装此模块须要先安装 sregex
运行库:nginx
$ git clone https://github.com/agentzh/sregex $ cd sregex $ make $ make install
软连接:git
$ ln -sv /usr/local/lib/libsregex.so.0.0.1 /lib64/libsregex.so.0
而后获取 replace-filter-nginx-module
模块,并解压,无需安装:github
$ cd /root/soft/src $ git clone https://github.com/agentzh/replace-filter-nginx-module
获取Nginx源码:express
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz $ tar zxvf nginx-1.12.2.tar.gz
这里采用静态编译的方式安装模块,须要从新编译 nginx
。获取 nginx
上一次编译参数:json
$ 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
模块:bash
--add-module=/root/soft/src/replace-filter-nginx-module
最终编译命令:app
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
热重启就好了。
一、已安装nginx动态添加模块
http://coolnull.com/4245.html
二、LNMP平滑升级nginx并安装ngx_lua模块教程
http://www.mamicode.com/info-detail-1110824.html
三、openresty/replace-filter-nginx-module: Streaming regular expression replacement in response bodies
https://github.com/openresty/replace-filter-nginx-module#synopsis
四、比ngx_http_substitutions_filter_module 更强大的替换模块sregex的replace-filter-nginx-module
http://www.javashuo.com/article/p-ppkbyfxl-ds.html
五、Nginx编译安装Lua - 飞鸿影~ - 博客园
http://www.javashuo.com/article/p-uyselemm-dh.html