Home Operation and Maintenance Nginx How to configure Nginx to record and analyze slow response requests and replace website response content

How to configure Nginx to record and analyze slow response requests and replace website response content

May 12, 2023 pm 08:16 PM
nginx


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
Copy after login

2. Instruction log_request_speed
2.1 log_request_speed_filter
Syntax:

 log_request_speed_filter [on|off]
Copy after login

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]
Copy after login

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;
   ...
}
Copy after login

The slow requests recorded in the error log are as follows

How to configure Nginx to record and analyze slow response requests and replace website response content

##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
Copy after login
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
Copy after login

From 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
Copy after login

  • -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;
Copy after login

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;
Copy after login

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;
Copy after login

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 ...;
Copy after login

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 &#39;本站&#39;;
    sub_filter_types text/html;
    sub_filter_once on;
  }
}
Copy after login

2.2 Test
The content is as follows

# cat /data/site/www.jb51.net/2013/10/20131001_sub1.html
Copy after login
welcome to jb51!
jb51 team!
Copy after login

Access results

# curl www.jb51.net/2013/10/20131001_sub1.html
Copy after login
Copy after login


welcome to 本站!
jb51 team!
Copy after login

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 &#39;本站&#39;;
  sub_filter_once off;
}
Copy after login

Then test

# curl www.jb51.net/2013/10/20131001_sub1.html
Copy after login
Copy after login
welcome to 本站!
本站 team!
Copy after login

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> &#39;</head><script language="javascript" src="$script"></script>&#39;;
  sub_filter_once on;
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

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 check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

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.

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

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.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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).

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

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

How to run nginx apache How to run nginx apache Apr 14, 2025 pm 12:33 PM

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.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

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.

See all articles