What this article shares with you is that nginx uses replace-filter-nginx-module to implement content replacement. It is very detailed. Friends in need can refer to it.
Sometimes we want to respond (such as PHP interface) The returned content is made into a string. Although you can use methods related to each language code (such as PHP's str_replace
) to replace it, it is more convenient to replace it at the nginx level without modifying the code.
Convention: The source code directory of this article is placed in: /root/soft/src
.
To install this module, you need to install sregex
runtime library:
$ git clone https://github.com/agentzh/sregex $ cd sregex $ make $ make install
Then install 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 using static compilation and needs to be recompilednginx
. Get nginx
Last compilation parameters:
$ 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
Add herereplace-filter-nginx-module
Module:
--add-module=/root/soft/src/replace-filter-nginx-module
Final compilation 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 upgrading nginx, do not make install
, otherwise it will be overwritten. Next, replace the binary file manually:
$ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak $ cp -rfp ./objs/nginx /usr/local/nginx/sbin/
Test whether it is feasible:
$ nginx -v nginx version: nginx/1.12.2
Is the configuration 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
What is tested here is : Replace 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:9000; fastcgi_index index.php; include fastcgi.conf; }
is:
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 addreplace_filter_types
.
After saving, check whether the configuration is OK:
$ /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
Then hot restart:
# 升级完成第一次需要重启 $ service nginx restart # 热重启 $ /usr/local/nginx/sbin/nginx -s reload
1. nginx dynamically added module is installed
http://coolnull.com/4245.html
2. LNMP smoothly upgrade nginx and install ngx_lua module tutorial
http://www.mamicode.com/info-.. .
3. openresty/replace-filter-nginx-module: Streaming regular expression replacement in response bodies
https://github.com/openresty/...
4. A more powerful replacement module than ngx_http_substitutions_filter_module, replace-filter-nginx-module of sregex
https://www.cnblogs.com/archo...
Original version At: http://www.cnblogs.com/52fhy/...
Related recommendations:
nginxHow to modify the upload file size
nginx configuration PHP instance
curl access local timeout in PHP + Nginx environment
The above is the detailed content of nginx uses replace-filter-nginx-module to implement content replacement. For more information, please follow other related articles on the PHP Chinese website!