nginx implements an example of content replacement using replace filter nginx module

  • 2020-05-14 06:02:16
  • OfStack

Sometimes we want to string the content returned by the response (such as the PHP interface), and while you can replace it with a code-related method in each language (such as str_replace for PHP), it is more convenient to replace it at the nginx level without changing the code.

Convention: this article source code directory 1 in: /root/soft/src.

The installation

To install this module, you need to install the sregex runtime:


$ git clone https://github.com/agentzh/sregex
$ cd sregex
$ make
$ make install

Then install the replace-filter-nginx-module module:


$ cd /root/soft/src
$ git clone https://github.com/agentzh/replace-filter-nginx-module
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ tar zxvf nginx-1.12.2.tar.gz

To install the module statically, you need to recompile nginx. Get the 1 compilation parameter on nginx:


$ 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 

Here add the replace-filter-nginx-module module:


--add-module=/root/soft/src/replace-filter-nginx-module

Final build command:


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
 
$ make

Since this is an nginx upgrade, don't make install, otherwise it will be covered. Next, manually replace the base 2 file:


$ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
$ cp -rfp ./objs/nginx /usr/local/nginx/sbin/

Whether the test is feasible:


$ nginx -v
nginx version: nginx/1.12.2

Whether the configuration is normal:


$ /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

configuration

Here's the test: replace the img.test.com returned by the interface with media.test.com.

Modification: / 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;
}

To:


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;

Note that you need to add replace_filter_types.

Check if the configuration is ok after saving:


$ cd /root/soft/src
$ git clone https://github.com/agentzh/replace-filter-nginx-module
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ tar zxvf nginx-1.12.2.tar.gz
0

Then hot reboot:


$ cd /root/soft/src
$ git clone https://github.com/agentzh/replace-filter-nginx-module
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ tar zxvf nginx-1.12.2.tar.gz
1

Related articles: