本文主要讲 如何全局替换站点指定字符串、若是制做docker容器镜像、如何使用dockerfile制做镜像、nginx含插件编译。
复制代码
前段时间某个场景须要将咱们产品中的一些文字替换成另外的一些文字,好比原来全站http改成https(或者将http://改成//);好比CDN节点地址替换;好比原来是AAA产品,如今须要改成BBB产品;好比原来网站名叫CCC,如今改成DDD等。相似的需求,若是一个个改,会耗费很多时间,并且不免会遗漏。因而,临时解决方案:使用nginx的ngx_http_substitutions_filter_module
模块进行全局替换。javascript
可能有人说,这样替换性能不是有损耗吗?我以为能够限定http返回头(application/xml
application/javascript
text/css text/xml
),对于图片啊,文件啊啥的不替换;其次,也能够提升nginx在集群中的副本数量。css
ngx_http_substitutions_filter_module是一个可替换内容的nginx插件,html
subs_filter source_str destination_str [参数]java
支持过滤mine内容类型: subs_filter_types mime-type [mime-types]
nginx
github地址c++
说明文档git
本文均使用docker环境下编译,非docker环境,只需忽略docker基础镜像部分便可。github
docker run --name subs_filter_ng -i -t -p 80:80 centos:latest /bin/bash
复制代码
yum -y install wget
yum -y install unzip
yum -y install gcc-c++
yum -y install gcc automake autoconf libtool make
复制代码
cd ~
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
tar zxvf pcre-8.37.tar.gz
wget https://www.openssl.org/source/openssl-1.0.1q.tar.gz
tar zxvf openssl-1.0.1q.tar.gz
wget -O ngx_http_substitutions_filter_module-master.zip https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip
unzip ngx_http_substitutions_filter_module-master.zip
复制代码
cd /root/nginx-1.8.0
./configure --sbin-path=/root/nginx-1.8.0/nginx --conf-path=/root/nginx-1.8.0/nginx.conf --pid-path=/root/nginx-1.8.0/nginx.pid --with-http_ssl_module --with-pcre=/root/pcre-8.37 --with-zlib=/root/zlib-1.2.11 --with-openssl=/root/openssl-1.0.1q --with-http_stub_status_module --add-module=/root/ngx_http_substitutions_filter_module-master/ --prefix=/root/nginx-1.8.0
make
make install
复制代码
修改 /root/nginx-1.8.0/nginx.conf 文件。正则表达式
能够放到server里,也能够放入http、location。docker
subs_filter_types application/xml application/javascript text/css text/xml;
subs_filter 'http' 'https';
复制代码
记得把第一行换位user root;
,要去掉前面注释。
./nginx -c /root/nginx-1.8.0/nginx.conf
复制代码
cat << EOF > /root/.vimrc
:set encoding=utf-8
:set fileencodings=ucs-bom,utf-8,cp936
:set fileencoding=gb2312
:set termencoding=utf-8
EOF
复制代码
docker commit -m "nginx" b668 nginx/subs_filter_nginx:v1
复制代码
其中b668为容器id
这里能够将上述脚本编写为一个dockerfile文件,方便之后使用。
这样一个能够替换内容的nginx镜像就诞生了,但愿经过本文的例子能帮助你们理解nginx编译过程(毕竟nginx不像其余apache啥的增长模块只须要引入文件便可,nginx增长模块都须要从新编译)。