nginx 替换html字符串,随手打造一个能够替换全站字符串的nginx镜像(docker)

本文介绍了如何使用nginx的ngx_http_substitutions_filter_module模块进行全局字符串替换,包括正则替换和不同替换逻辑。通过编译自定义的nginx镜像并创建dockerfile,可以方便地在docker环境中实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要讲 如何全局替换站点指定字符串、若是制做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

认识nginx内容替换模块

ngx_http_substitutions_filter_module是一个可替换内容的nginx插件,html

可支持:

中英文替换

正则替换

支持多种替换逻辑

具体替换逻辑及使用以下以下:

subs_filter source_str destination_str [参数]java

g(default): 所有替换

i: 不准分大小写替换.

o: 只替换一次

r: 正则表达式

做用域:

http

server

location

支持过滤mine内容类型: subs_filter_types mime-type [mime-types]nginx

编译

本文均使用docker环境下编译,非docker环境,只需忽略docker基础镜像部分便可。github

使用centos基础镜像

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

复制代码

vi不支持中文解决方案

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

这里能够将上述脚本编写为一个dockerfile文件,方便之后使用。

总结

这样一个能够替换内容的nginx镜像就诞生了,但愿经过本文的例子能帮助你们理解nginx编译过程(毕竟nginx不像其余apache啥的增长模块只须要引入文件便可,nginx增长模块都须要从新编译)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值