Nginx uses Replace-filter-nginx-module to implement content substitution

Source: Internet
Author: User
This article gives you to share is Nginx use Replace-filter-nginx-module implementation of content replacement, very detailed, the need for friends can refer to

sometimes we want to make some strings for the content returned by the response (for example, the PHP interface), although it can be replaced with various language code-related methods (such as PHP str_replace ), but it is more convenient to replace at nginx level without modifying the code.

Convention: This article source code directory Unified in: /root/soft/src .

Installation

Installing this module requires that you first install the sregex Runtime Library:

$ 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

This is a statically compiled installation of the module and needs to be recompiled nginx . Gets nginx The last compilation parameter:

$ 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 1 1 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_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

because this is the upgrade nginx, so do not make install , otherwise it is really covered. 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/

Testing is possible:

$ Nginx-vnginx version:nginx/1.12.2

Configuration is OK:

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

Here is the test: Replace the interface return img.test.com 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;}

For:

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 that 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 the hot restart:

# Upgrade completed for the first time requires a restart of service nginx Restart  # Hot Restart $/usr/local/nginx/sbin/nginx-s Reload

Reference

1, installed Nginx dynamic Add module
Http://coolnull.com/4245.html
2, LNMP Smooth 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, more powerful than Ngx_http_substitutions_filter_module replacement module Sregex replace-filter-nginx-module
Https://www.cnblogs.com/archo ...

Originally contained in: http://www.cnblogs.com/52fhy/...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.