Full series of PHP video tutorials: Detailed PHP – http://www.xishuophp.com/
Nginx (“engine x”) is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server. Nginx was developed by Igor Sysoev for the second most visited Rambler.ru site in Russia. The first public version 0.1.0 was released on October 4, 2004. It releases its source code under a BSD-like license and is known for its stability, rich feature set, sample configuration files, and low system resource consumption. On June 1, 2011, nginx 1.0.4 was released.
Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server released under a BSD-like protocol. Developed by Russian programmer Igor Sysoev, it is used by Russia’s large portal and search engine Rambler (Russian: Рамблер). Its characteristic is that it occupies less memory and has strong concurrency capability. In fact, the concurrency capability of nginx is indeed better than other web servers of the same type. Users of nginx websites in mainland China include: Baidu, Sina, Netease, Tencent, etc.
If nginx is attacked or the traffic suddenly increases, nginx will cause the server to go down due to high load or insufficient memory, and eventually the site will be inaccessible. The solution I’m going to talk about today comes from the nginx-http-sysguard module developed by Taobao, which is mainly used to perform corresponding actions when the load and memory reach a certain threshold, such as directly returning 503, 504 or others. Wait until the memory Or the load returns to the range of the threshold, and the site is available again. Simply put, these modules allow nginx to have a buffer time, slowly.
1. Installation
Download nginx-1.4.1 in the attachment and change the extension to .tar.gz
1 2 |
tar-zxvf nginx-1.4.1.tar.gz wget https://github.com/alibaba/nginx-http-sysguard/archive/master.zip -O nginx-http-sysguard-master.zip |
The download module can also be downloaded in the attachment.
1 2 3 |
unzip nginx-http-sysguard-master.zip cdnignx-1.4.1 patch -p1 <../nginx-http-sysguard-master/nginx_sysguard_1.3.9.patch |
If you have installed nginx before, you can view the installation command with nginx -V, then install it again and add the –add-module=../nginx-http-sysguard module
1 | make && make install |
2. Explanation
sysguard command
Syntax: sysguard [on |off]
Default: sysguard off
Configuration section: http, server, location
switch module
Syntax: sysguard_loadload=number [action=/url]
Default: none
Configuration section: http, server, location
Specify the load threshold. When the load of the system exceeds this value, all requests will be redirected to the uri request defined by the action. If the URL action is not defined, the server will return 503 directly
Syntax: sysguard_memswapratio=ratio% [action=/url]
Default: none
Configuration section: http, server, location
Define the threshold used by the swap partition. If the swap partition exceeds this threshold, all subsequent requests will be redirected to the uri request defined by the action. If the URL action is not defined, the server will return 503 directly
Syntax: sysguard_intervaltime
Default: sysguard_interval1s
Configuration section: http, server, location
Define the frequency of system information update, the default is 1 second.
Syntax: sysguard_log_level info | notice | warn | error
Default: sysguard_log_level error
Configuration section: http, server, location
Define the log level of sysguard
3. Configuration
The server section increases:
1 2 3 4 5 6 7 8 9 10 11 |
sysguard on; # For the convenience of testing, the load threshold is 0.01 (representing the load average: the leftmost value), which is usually around the number of cpu cores. sysguard_loadload=0.01 action=/loadlimit; sysguard_memswapratio=20% action=/swaplimit; location/loadlimit{ return404; #This is convenient for testing and defining 404 errors; } location/swaplimit{ return503; } |
4. Test
Use webbench to increase the load of the machine, and you will find a 404 error when you open the page.
V. Summary
In the case where nginx is a realserver, I personally recommend using this method. Once the server load climbs, it usually takes a long time to return to the normal level. In the case of using this plug-in, the load reaches the threshold, nginx Return 503, the previous section uses failover to send requests to other servers, and this server can quickly return to normal levels without access, and can start working immediately. The processing speed of the server exceeding the threshold will also be greatly reduced. Using this module, the request is cleverly sent to a faster server, which avoids the problem of slow access speed to a certain extent. The above mentioned is in the cluster environment. In a single-point environment, everyone should consider whether it is necessary.
If you want to experience the novice of the LINUX system in depth, you can also download a Fangde Linux software center and try it out first.
gino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;font-size:16px;background-color:#FFFFFF;”>
Define the log level of sysguard
3. Configuration
The server section increases:
1 2 3 4 5 6 7 8 9 10 11 |
sysguard on; # For the convenience of testing, the load threshold is 0.01 (representing the load average: the leftmost value), which is usually around the number of cpu cores. sysguard_loadload=0.01 action=/loadlimit; sysguard_memswapratio=20% action=/swaplimit; location/loadlimit{ return404; #This is convenient for testing and defining 404 errors; } location/swaplimit{ return503; } |
4. Test
Use webbench to increase the load of the machine, and you will find a 404 error when you open the page.
V. Summary
In the case where nginx is a realserver, I personally recommend using this method. Once the server load climbs, it usually takes a long time to return to the normal level. In the case of using this plug-in, the load reaches the threshold, nginx Return 503, the previous section uses failover to send requests to other servers, and this server can quickly return to normal levels without access, and can start working immediately. The processing speed of the server exceeding the threshold will also be greatly reduced. Using this module, the request is cleverly sent to a faster server, which avoids the problem of slow access speed to a certain extent. The above mentioned is in the cluster environment. In a single-point environment, everyone should consider whether it is necessary.
If you want to experience the novice of the LINUX system in depth, you can also download a Fangde Linux software center and try it out first.