


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
- -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;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!

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINXUnit is an open source application server that supports multiple programming languages and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
