Home > Backend Development > PHP Tutorial > nginx uses replace-filter-nginx-module to implement content replacement

nginx uses replace-filter-nginx-module to implement content replacement

不言
Release: 2023-03-22 19:02:01
Original
2649 people have browsed it

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.

Installation

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

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

Then install replace-filter-nginx-moduleModule:

$ 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
Copy after login

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
Copy after login

Add herereplace-filter-nginx-module Module:

--add-module=/root/soft/src/replace-filter-nginx-module
Copy after login

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
Copy after login

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/
Copy after login

Test whether it is feasible:

$ nginx -v
nginx version: nginx/1.12.2
Copy after login

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
Copy after login
Copy after login

Configuration

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;
}
Copy after login

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;
Copy after login

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
Copy after login
Copy after login

Then hot restart:

# 升级完成第一次需要重启
$ service nginx restart 
 
# 热重启
$ /usr/local/nginx/sbin/nginx -s reload
Copy after login

Reference

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template