|
|
Posted on 4/2/2019 11:47:27 AM
|
|
|
|

Nginx is a lightweight and high-performance server software, although lightweight, but 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 demand and needed to use the nginx web content replacement module, the following is a record of the relevant experiment, posted to communicate with you, if there are any shortcomings, please point out.
ngx_http_sub_module module is a filter that modifies strings in the website's response content. This module is already built into nginx, but it is not installed by default, and you need to add configuration parameters to install it: --with-http_sub_module If you have already installed nginx, you only need to add this module.
1. Installation:
nginx official website download and installation package:The hyperlink login is visible.
# wgetThe hyperlink login is visible.
# tar -zxvf nginx-1.11.5.tar.gz
# cd nginx-1.11.5
# ./configure --with-http_stub_status_module --with-http_sub_module && make && make install
2. Common instructions:
2.1 sub_filter Instructions: sub_filter string (original string) replacement (string to replace);
For setting the need to replace the description string with the description string. String is the string to be replaced, replacement is the new string, which can have variables in it.
2.2 sub_filter_last_modified Instruction: sub_filter_last_modified on | off;
It is used to set whether to modify whether to modify the replacement in the web page can be configured in the three locations of http, server, and location in nginx.conf, and the default value is off.
2.3 sub_filter_onceDirective: sub_filter_once on | off;
Used to set the number of string replacements,By default, it is replaced only once. If it is on, only the first matched to character will be replaced by default, and if it is off, then all matched characters will be replaced;
2.4 sub_filter_types Instruction: sub_filter_types *
Used to specify the MIME type to be replaced, the default is "text/html", if set to *, then all;
Note: The above command can be configured in http, server, and location in nginx.conf.
3. Reverse proxy dynamic replacement of web content example reference:
upstreamThe hyperlink login is visible. {
server 118.184.180.46:80;
}
#反向代理, if you have multiple server IPs, you can also add policies such as load balancing method and weight #
server {
listen 80;
#监听端口 #
server_nameThe hyperlink login is visible.;
#设置server name #
charset utf-8;
#设置字符编码为utf-8, which can be adjusted according to the actual situation #
location / {
proxy_passThe hyperlink login is visible.$request_uri;
#反向代理规则 #
proxy_set_header Accept-Encoding deflate;
#设置反向代理头部, sometimes the origin server responds to the gzip format, and there will be problems when replacing it, which can be solved by this item #
subs_filter '183.251.160.127' '123.181.128.17';
#替换指定ip, replace 183.251.160.127 with 123.181.128.17 here #
subs_filter 'Fujian Province Longyan City Mobile' 'Hebei Province Tangshan City Telecom';
#替换城市. Operator information, this replacement module of nginx supports Chinese replacement #
subs_filter_types text/html;
#指定被替换的MIME类型 #
sub_filter_once on;
#指定字符串替换次数, on means to replace only the first matched character, and off means to replace all matched characters #
}
}
Illustrate:
1. This module is not case-sensitive;
2. Support Chinese substitution;
Nginx.conf full configuration:
Screenshot of the experimental effect:
Conclusion:
Another similar content replacement module nginx_substitutions_filter, if you are interested, you can experiment and play.
Renderings:
Configure code:
|
Previous:The difference between Spring JPA save and saveAndFlushNext:IT blacklist query, netizens broke the news and shared, for reference only
|