Author: Uncle Sunan [Beijing, non famous Internet practitioners]
Source: How nginx adds substitutions_filter module, subs_ The filter instruction is replaced multiple times
The article continues to talk about the anti generation function of nginx. The majority of readers already know that nginx can use sub_ The filter instruction replaces the text in the returned content. However, there is a limitation to this: only a set of replacement values can be set. So, what if two or even N groups of data need to be replaced? Of course, if you use a back-end language such as php, this is not a thing at all. However, this is simply using nginx to deal with this kind of thing.
How nginx adds substitutions_filter module, subs_ The filter instruction is replaced many times (Figure 4-1)
The answer to this requirement is: substitutions_filter module, which is not the official module of nginx, but belongs to a third-party module. Therefore, nginx needs to be recompiled here.
The test environment in this paper is CentOS 7.5, pagoda 5.9.0, nginx 1.14, substitutions_filter 0.64.
Find the location of nginx source code
The writing environment of this article is based on pagoda 5.9.0 under CentOS 7.5. Then the code of nginx is also provided by the pagoda. The source code of nginx in the pagoda is located at: / www/server/nginx/src /. Write down this path. If you can tell why this directory is the source directory of nginx, you can see if there is a makefile file under this directory, which is an obvious sign.
How nginx adds substitutions_filter module, subs_ The filter instruction is replaced multiple times (Figure 4-2)
View the compilation parameters of nginx
Because we all know that the compilation options of nginx are different, and the resulting executable files are also quite different. Therefore, it is very necessary to check the compilation options of nginx here. The way to view is simple. The code is as follows:
Bash
nginx -V
Note that here is - V, not - V. Is the capital letter V, otherwise you will not get detailed compilation parameter information.
The return value is similar to the following:
Bash
nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2l 25 May 2017 TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/www/server/nginx --with-openssl=/www/server/nginx/src/openssl --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --add-module=/www/server/nginx/src/nginx-http-concat --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-pcre=pcre-8.40 --with-ld-opt=-ljemalloc
The value after configure arguments in the above content is the target string of Uncle Sunan. Write down these options for backup.
Download sub module code
substitutions_filter_module project address is:
As of press time, the latest version is 0.6.4. It seems that the author hasn't updated the code for several years.
How nginx adds substitutions_filter module, subs_ The filter instruction is replaced many times (Figure 4-3)
Bash
cd <nginx_src_folder> git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git ./configure <Original configure arguments> --add-module=<nginx_src_folder>/ngx_http_substitutions_filter_module make&&make install
Note that < original configuration arguments > should be replaced with the original configuration items. Replace < nginx_ src_ Folder > is your real source directory. In this article, this value is: / www/server/nginx/src /.
If this is the second installation, the cleaning commands that may be required are:
Bash
cd /www/server/nginx/src/ make clean rm -rf ngx_http_substitutions_filter_module/
For example, for uncle Sunan, the command line should be as follows:
Bash
cd /www/server/nginx/src/ git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git ./configure --user=www --group=www --prefix=/www/server/nginx --with-openssl=/www/server/nginx/src/openssl --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --add-module=/www/server/nginx/src/nginx-http-concat --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-pcre=pcre-8.40 --with-ld-opt=-ljemalloc --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module make&&make install
When configuring, be sure to check the output log. Some parameters may be inappropriate and need to be deleted. Generally speaking, there will be no problem if the compilation parameters are obtained in strict accordance with nginx -V.
The compilation process is quite long. After an extremely long wait, uncle Sunan found the newly generated nginx file under / www/server/nginx/src/objs /.
How nginx adds substitutions_filter module, subs_ The filter instruction is replaced many times (Figure 4-4)
How does nginx work
Normally, it will take effect after install.
Bash
make install
The effective nginx location is: / www/server/nginx/sbin/nginx.
If it doesn't take effect, remedies
However, if it does not take effect, we can also operate it manually.
Step 1: you can obtain the current nginx location through the which command. Note that the resulting path values may differ.
Bash
which nginx
Return value:
Bash
/usr/bin/nginx
Step 2: manually replace nginx:
Bash
systemctl stop nginx rm -rf /usr/bin/nginx_old mv /usr/bin/nginx /usr/bin/nginx_old cp /www/server/nginx/src/objs/nginx /usr/bin/ systemctl start nginx
Usage example
The new instruction is sub_filter: sub_filter. Then, you can set the subs multiple times in the nginx configuration_ Filter. Regular expressions and variables can also be used in configuration.
subs_filter_types text/html text/css text/xml; subs_filter st(\d*).example.com $1.example.com ir; subs_filter a.example.com s.example.com; subs_filter http://$host https://$host;
Here, uncle Sunan recommends that you go back to nginx's method of setting up agents.