Nginx is a lightweight high-performance server software. Although it is lightweight, it is very powerful. It can be used to provide WEB services, reverse proxy, load balancing, caching services, and even build rtmp streaming services by adding some modules. Recently I encountered a customer's demand and needed to use the nginx web content replacement module and post it to communicate with everyone. If there are any shortcomings, please point it out.
The ngx_http_sub_module module is a filter that modifies strings in the website response content. This module is already built into nginx, but it is not installed by default. If you need to install it, you need to add configuration parameters: --with-http_sub_module. If you have installed nginx, you only need to add this module.
Install and replace
nginx official website download and installation package: /en/
# wget /download/nginx-1.11.
# tar -zxvf nginx-1.11.
# cd nginx-1.11.5
# ./configure --with-http_stub_status_module --with-http_sub_module && make && make install
Common instructions
sub_filter directive
sub_filter string (original string) replace (the string used for replacement);
Used to set the instruction string that needs to be used to replace the instruction string. string is the string to be replaced, replacement is the new string, which can contain variables.
Sub_filter_last_modified directive
sub_filter_last_modified on | off;
Used to set whether to modify the web page after replacing it. It can be configured and used in three locations: http, server, and location. The default value is off;
sub_filter_once directive
sub_filter_once on | off;
Used to set the number of string replacements, and only replaces once by default. If it is on, only the first matched characters will be replaced by default. If it is off, all matching characters will be replaced;
sub_filter_types directive
sub_filter_types *
Used to specify the MIME type that needs to be replaced, default is "text/html", if it is formulated as *, then all;
Note: The above instructions can be configured and used in three locations: http, server, and location;
Reverse proxy dynamically replace web content instance reference
server { listen 80; server_name ; # Upload file size limit client_max_body_size 20M; # Set to on to indicate the mode of starting efficient file transfer sendfile on; #charset koi8-r; #access_log logs/ main; location / { #access_log logs/ main; location / { #root html; #index ; proxy_pass ; proxy_set_header Host ; proxy_set_header Referer ; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; #proxy_request_buffering off; proxy_set_header Accept-Encoding ""; sub_filter_types *; sub_filter_once off; #sub_filter_last_modified on; sub_filter '' ''; sub_filter '' ''; sub_filter '' ''; sub_filter '' ''; sub_filter '' ''; sub_filter '' ''; sub_filter '' ''; sub_filter '' ''; #sub_filter '.' ''; #sub_filter '' ''; } #location ~ \.php$ { # proxy_pass ; #} #location ~ /$ { # proxy_pass ; #} }
Parameter explanation
Note that multiple sub_filters are only supported in the new version of nginx.
proxy_set_header Accept-Encoding "";
The reason for setting this is: telling the backend not to perform gzip compression. If it is a gzip compression stream, then we cannot replace it.
sub_filter_types *;
All request response types are replaced by sub_filter specified.
sub_filter_once off;
The sub_filter will be executed multiple times instead of once. The effect is similar to that in java instead of replace.
sub_filter 'str1' 'str2';
Replace string, str1 is the searched string, str2 is the string that was finally replaced
Summarize
This is the article about the Nginx reverse proxy and content replacement module realizing the dynamic replacement function of web page content. For more information about Nginx reverse proxy replacement web page string content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!