An example of nginx using replace-filter-nginx-module to implement content substitution
Sometimes we want to make some strings for the contents returned by the response (such as the PHP interface). Although we can replace them with code-related methods in various languages (such as PHP's str_replace), it is more convenient to replace at the nginx level without modifying the code.
Convention: this article source code directory is uniformly placed in: / root/soft/src.
Installation
Installing this module requires the sregex runtime to be installed first:
$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
The module is installed statically here, and the nginx needs to be recompiled. Get the parameters for the last compilation of nginx:
Nginx-Vnginx version: nginx/1.12.2built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure 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
Add the replace-filter-nginx-module module here:
-- add-module=/root/soft/src/replace-filter-nginx-module
Final compile command:
Cd nginx-1.12.2 $. / configure-- user=www-- group=www-- prefix=/usr/local/nginx-- with-http_stub_status_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 upgrade of nginx, do not make install, or you will really overwrite it. Next, replace the binaries manually:
$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-vnginx version: nginx/1.12.2
Whether the configuration is normal:
$/ usr/local/nginx/sbin/nginx-tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful
Configuration
The test here is to replace the img.test.com returned by the interface with media.test.com.
Modify: / 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 uri 9000; fastcgi_index index.php; include fastcgi.conf;}
Are:
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 media.test.com' 9000; fastcgi_index index.php; include fastcgi.conf
Note that you need to add replace_filter_types.
Check whether the configuration is ok after saving:
$/ usr/local/nginx/sbin/nginx-tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful
Then hot restart:
# restart $service nginx restart # Hot restart $/ usr/local/nginx/sbin/nginx-s reload for the first time after the upgrade is completed
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.