


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 winner
3.3 Analyze script syntax
# ./analyzer.pl -h
- -h : this help message #Display help message
- -u : group by upstream # Press upstream grouping
- -o : group by host #Group by host
- -r : group by request #Group by request, recommend this
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;
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;
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;
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 ...;
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!
# curl www.jb51.net/2013/10/20131001_sub1.html
welcome to 本站! jb51 team!
location / { sub_filter jb51 '本站'; sub_filter_once off; }
# curl www.jb51.net/2013/10/20131001_sub1.html
welcome to 本站! 本站 team!
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.
