Home >Operation and Maintenance >Nginx >How to configure Nginx to record and analyze slow response requests and replace website response content
1. Module installation
nginx third-party module installation method is skipped here.
Configuration parameters
./configure --prefix=/usr/local/nginx-1.4.1 --with-http_stub_status_module \ --add-module=../ngx_http_log_request_speed
2. Instruction log_request_speed
2.1 log_request_speed_filter
Syntax:
log_request_speed_filter [on|off]
Configuration section: n/a
context: location , server, http
Enable or disable module
2.2 log_request_speed_filter_timeout
Syntax:
log_request_speed_filter_timeout [num sec]
Default: 5 seconds
Configuration section: location, server, http
This is not a real timeout, but it means that when the request exceeds the time given here, it will be recorded in the nginx error log. The default value is 5000 microseconds (5 seconds), if a request is less than 5 seconds , this request will not be recorded in the log, but if it exceeds 5 seconds, the request will be recorded in the nginx error log
3. Usage example
3.1 nginx configuration
http{ log_request_speed_filter on; log_request_speed_filter_timeout 3; ... }
The slow requests recorded in the error log are as follows
##3.2 Log analysis
cd /usr/local/nginx-1.4.1/logs wget http://wiki.nginx.org/images/a/a8/log_analyzer.tar.gz tar -xzvf log_analyzer.tar.gz cd request_speed_log_analyzer # cat ../error.log | grep 'process request'| ./analyzer.pl -r
post /wp-admin/admin-ajax.php http/1.1 --- avg ms: 1182, value count: 2 get /shmb/1145.html http/1.1 --- avg ms: 2976, value count: 1 <--- the winnerFrom the log , we found that there are 2 slow requests here, the slowest one is /shmb/1145.html, and it is also marked "the winner", author, you win. Very humorous.
3.3 Analyze script syntax
# ./analyzer.pl -h
4. nginx test version
Currently the author only tests under 0.6.35 and 0.7.64, and does not guarantee that it can be used in other environments. My current test version is 1.4.1, which is currently in normal use. Please test it before using it.nginx replaces the website response content (ngx_http_sub_module)
The ngx_http_sub_module module is a filter that modifies the string in the website response content. For example, you want to 'jb51' in the response content 'Replace all with 'this site'. This module has been built into nginx, but is not installed by default. To install it, you need to add configuration parameters: --with-http_sub_module
1. Directives
Syntax:
sub_filter string replacement;Default value: —
Configuration section: http, server, location
Settings need to use the description string to replace the description string. string is the string to be replaced, Replacement is a new string, which can contain variables.
Syntax:
sub_filter_last_modified on | off;Default value: sub_filter_last_modified off;
Configuration section: http, server, location
This command was added in nginx 1.5.1. I don’t have it in this version, so you can ignore it. .
allows preserving the “last-modified” header field from the original response during replacement to facilitate response caching.
by default, the header field is removed as contents of the response are modified during processing.
Syntax :
sub_filter_once on | off;Default value: sub_filter_once on;
Configuration section: http, server, location
Replace the string once or multiple times, the default is to replace once, for example, you want to replace jb51 in the response content For this site, if multiple jb51s appear, only the first one will be replaced. If off, then all jb51s will be replaced.
Syntax:
sub_filter_types mime-type ...;Default value: sub_filter_types text/html;
Configuration section: http, server, location
Specify the mime type that needs to be replaced. The default is "text/html". If it is specified as *, then all
2. nginx replacement string instance 2.1 Configuration
server { listen 80; server_name www.jb51.net; root /data/site/www.jb51.net; location / { sub_filter jb51 '本站'; sub_filter_types text/html; sub_filter_once on; } }
2.2 Test
The content is as follows
# cat /data/site/www.jb51.net/2013/10/20131001_sub1.html
welcome to jb51! jb51 team!Access results
# curl www.jb51.net/2013/10/20131001_sub1.html
welcome to 本站! jb51 team!We can see that the replacement is case-insensitive, and jb51 is only replaced once. I tried changing sub_filter_once on to off.
location / { sub_filter jb51 '本站'; sub_filter_once off; }Then test
# curl www.jb51.net/2013/10/20131001_sub1.html
welcome to 本站! 本站 team!We can see that jb51 has been replaced.
For example, if you want to append a piece of js after , the configuration is as follows:
location / { sub_filter </head> '</head><script language="javascript" src="$script"></script>'; sub_filter_once on; }
The above is the detailed content of How to configure Nginx to record and analyze slow response requests and replace website response content. For more information, please follow other related articles on the PHP Chinese website!