Nginx record analysis responds to slow requests and replaces the configuration of site response content _nginx

Source: Internet
Author: User
Tags curl perl script

Nginx Record Analysis Web site response slow request (Ngx_http_log_request_speed)
Nginx Module ngx_http_log_request_speed can be used to find out what the Web site requests are slow, for a lot of sites, files and requests to find out which requests are relatively slow, This plugin is very effective. The author's original intention was to write to his own, to find the site processing time of the request, these requests are caused by high server load of a large source. After logging, you can know which requests need to be corrected by using the Perl script analysis log.
1. Module installation
nginx third-party module installation method Here's a bit of a stroke.
Configuration parameters

./configure--prefix=/usr/local/nginx-1.4.1--with-http_stub_status_module \
 --add-module=. /ngx_http_log_request_speed

2. Directive Log_request_speed
2.1 Log_request_speed_filter
Syntax:

 Log_request_speed_filter [On|off]

Configuration segment: N/A
Context:location, server, HTTP
To start or disable a module
2.2 Log_request_speed_filter_timeout
Syntax:

Log_request_speed_filter_timeout [num sec]

Default: 5 Seconds
Configuration segment: location, server, HTTP
This is not really a time-out, but rather that when the request exceeds a given time here, it is logged in the Nginx error log. The default value is 5000 microseconds (5 seconds), and if a request is less than 5 seconds, the request is not logged, but if more than 5 seconds, the request will be logged to the Nginx error log
3. Use examples
3.1 Nginx Configuration

http{
   log_request_speed_filter on;
   Log_request_speed_filter_timeout 3;
   ...
}

The slow request recorded in the error log is 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 WINNER

From the log, we found that there are 2 requests slow, the slowest is/shmb/1145.html, but also marked "the WINNER", the author you win. Very humorous.
3.3 Parsing script syntax

#./analyzer.pl-h 
    • -h:this Help Message # Displays the helpful information
    • -u:group by upstream # grouped by upstream
    • -o:group by host # Grouped by hosts
    • -r:group by request # Group by requests, recommend this

4. Nginx Test Version

At present, the authors only test under the 0.6.35 and 0.7.64, do not guarantee that other environments can be used. My current test version is 1.4.1, the current use of normal, before use, please first Test.

Nginx Replace site response content (ngx_http_sub_module)
the Ngx_http_sub_module module is a filter that modifies the strings in the response content of the site, such as you want to replace the ' jb51 ' in the response to ' Cloud-dwelling community ', this module has been built into the Nginx, but the default is not installed, you need to install the required configuration parameters:--with-http_sub_module
1. Instruction (Directives)
Syntax:

Sub_filter string Replacement;

Default value:-
Configuration segment: HTTP, server, location
The setting requires a description string to be substituted for the description string. String is the one to be replaced, and replacement is a new string with variables inside it.
Grammar:

Sub_filter_last_modified on | Off

Default value: Sub_filter_last_modified off;
Configuration segment: HTTP, server, location
This instruction is added in Nginx 1.5.1, I do not have this version, can be ignored.
Allows preserving the "last-modified" header field from original response during replacement to facilitate response CA Ching.
By default, the header field is removed as contents of the response are modified during.
Grammar:

 Sub_filter_once on | Off

Default value: Sub_filter_once on;
Configuration segment: HTTP, server, location
string replacement or multiple substitution, the default replacement, for example, you want to replace the JB51 in the response content for the cloud-dwelling community, if there are multiple jb51 appear, then only replace the first, if off, then all the jb51 will be replaced
Grammar:

 Sub_filter_types Mime-type ...;

Default value: Sub_filter_types text/html;
Configuration segment: HTTP, server, location
Specifies the MIME type that needs to be replaced, default to "text/html", and if made to *, then all
2. Nginx substitution String Instance
2.1 Configuration

server {
  listen    ;
  server_name www.jb51.net;
 
  root/data/site/www.jb51.net;  
 
  Location/{
    sub_filter jb51 ' cloud-dwelling community ';
    Sub_filter_types text/html;
    Sub_filter_once on;
  }
}

2.2 Test
the contents are 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 cloud-dwelling community!
JB51 team!

We can see that the substitution is case-insensitive, and that the jb51 is only replaced once. I'll try sub_filter_once on.

Location/{
  sub_filter jb51 ' cloud-dwelling community ';
  Sub_filter_once off;

Then test

# Curl Www.jb51.net/2013/10/20131001_sub1.html      
Welcome to cloud-dwelling community!
Cloud-dwelling Community team!

We can see that the jb51 have been replaced.
For example, you want to append a section of JS after

Location/{
  sub_filter    
 

This way I will no longer do the test, we can test.

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.