This article is mainly about how to global replace site specified string, how to make docker container image, how to use Dockerfile to make image, nginx including plug-in compilation.Copy the code

preface

Some time ago, some words in our product need to be replaced with other words in a certain scene, for example, the original whole site HTTP is changed to HTTPS (or http://is changed to //); For example, CDN node address replacement; For example, the original AAA product needs to be changed to BBB product; For example, the original website named CCC, now changed to DDD and so on. Similar requirements, if changed one by one, will take a lot of time, and inevitably missed. Hence the temporary solution: use the NGx_HTTP_SUBSTITUtionS_filter_module of Nginx for global substitution.

Some might say, isn’t this a lossy substitution of performance? I think it is possible to restrict HTTP return headers (application/ XML Application /javascript text/ CSS text/ XML) to not replace images, files, etc. Second, you can also increase the number of nginx replicas in the cluster.

Learn about the Nginx content replacement module

Ngx_http_substitutions_filter_module is an nginx plug-in for substitutable content,

Can support:

  1. English replacement
  2. Regular replacement
  3. Supports multiple replacement logic

The specific replacement logic and usage are as follows:

Subs_filter source_str destination_str [parameter]

  1. G (default): replace all
  2. I: Case sensitive substitution is not allowed.
  3. O: Replace only once
  4. R: regular expression

Scope:

  1. http
  2. server
  3. location

Subs_filter_types MIme-type [mime-types]

Making the address

documentation

compile

This article uses docker environment to compile, not docker environment, just ignore the docker basic image part.

Use centos basic image

docker run --name subs_filter_ng -i -t  -p 80:80 centos:latest /bin/bash
Copy the code

Download required components

yum -y install wget
yum -y install unzip
yum -y install gcc-c++
yum -y install gcc automake autoconf libtool make
Copy the code

Download required files

cd~ wget http://nginx.org/download/nginx-1.8.0.tar.gz tar ZXVF nginx - 1.8.0 comes with. Tar. Gz wget http://www.zlib.net/zlib-1.2.11.tar.gz tar ZXVF zlib - 1.2.11. Tar. Gz wget HTTP: / / https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz Tar ZXVF pcl-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.zipCopy the code

compile

cd/ root/nginx - 1.8.0 comes with. / configure -- sbin - path = / root/nginx - 1.8.0 comes with/nginx - conf - path = / root/nginx - 1.8.0 comes with/nginx. Conf Pid - path = / root/nginx - 1.8.0 comes with/nginx. Pid - with - http_ssl_module - with - pcre = / root/pcre - 8.37 - with - zlib = / root/zlib - 1.2.11 - with openssl = / root/openssl - q - with - http_stub_status_module 1.0.1 --add-module=/root/ngx_http_substitutions_filter_module-master/ --prefix=/root/nginx-1.8.0 make make installCopy the code

configuration

Example Modify the /root/nginx-1.8.0/nginx.conf file.

You can put it in server, HTTP, location.

subs_filter_types application/xml application/javascript text/css text/xml;
subs_filter 'http' 'https';
Copy the code

Remember to swap the first line user root; To remove the preceding comment.

run

. / nginx - c/root/nginx - 1.8.0 comes with/nginx. ConfCopy the code

Vi does not support Chinese solutions

cat << EOF > /root/.vimrc

:set encoding=utf-8

:set fileencodings=ucs-bom,utf-8,cp936

:set fileencoding=gb2312

:set termencoding=utf-8

EOF

Copy the code

Pack as mirror image

docker commit -m "nginx" b668 nginx/subs_filter_nginx:v1
Copy the code

B668 is the container ID

dockerfile

You can write the above script here as a Dockerfile for later use.

conclusion

The result is an nginx image that can replace content. Hopefully, this example will help you understand the nginx compilation process (after all, nginx add-ons need to be recompiled, unlike other Apache add-ons that simply import files).