PHP does not have database connection pool how to break? High-load solution using Nginx ngx_http_limit_req_module modules in PHP environments

Source: Internet
Author: User
Tags connection pooling fpm

Online operation of a set of auxiliary system is used to change the open source of PHP, has not played a lot of PHP before, did not think that this thing still has many pits. Suddenly one day a user doing online activities to promote, and then in a short period of time into the tens of thousands of requests, and then the database connection exhausted, in a short period of time almost down the entire system. Causes the system to crash has many reasons, today mainly for PHP does not have the database connection pool the reason to analyze.

In PHP, a database connection is established when the request arrives and is released at the end of the request. If thousands of requests arrive at the same time, then thousands of database connections will be established, which is very scary. and PHP Wood has a better database connection pool driver scheme, so we have to find another way.

There are three ways to solve this problem:
1. Use MySQL proxy middleware. Mysql Proxy provides the function of connection pooling management. But we did not use this method, because the situation is urgent, no one is familiar with this thing.
2. Use PHP-FPM. PHP-FPM is a fastcgi process manager for PHP. Configuration allows you to control the number of processes that process PHP requests concurrently.
Specific can refer to: http://www.linuxde.net/2013/06/14638.html
But we don't use this scenario either, because the installation configuration process is cumbersome.
3. Use the Nginx Ngx_http_limit_req_module to control the request.
This module can limit the frequency of requests through custom key values. The restricted method is like a funnel, which handles the number of requests per second, then defers the exceeded request, and finally returns a rejection of the maximum direct 503.
We use this scenario because we need a simple configuration, and we have the flexibility to control the scenario where the request is restricted. For example, we do not restrict requests for static resources, but restrict PHP requests. You can also extract variable information from the URL address as a key to achieve a finer request limit.
The following part of our configuration for you to explain.

http {... limit_req_zone $limit _key zone=limit_one:50m; #定义limit_key为Key的变量名, for subsequent assignments, each key has its own counter. Limit_one is the name of the zone.
    Rate represents a maximum of 30 simultaneous requests per second.
              server {... if ($request _uri ~*. *php.*) {set $limit _one $binary _remote_addr; #对于全部PHP首先有个默认的Key, use the client's IP as key.
         The equivalent of each client IP will be within the zone limit.
              } if ($query _string ~*. *id/(\d+) \.php.*) {set $limit _one $;
        #提取id后面的值作为Key.
              } if ($query _string ~*. *APPID/WX (. *) \.html.*) {set $limit _one $;
        #提取appid作为Key.
        } limit_req Zone=limit_one burst=200; #限制limit_one在此server内的漏斗容量为200. Assuming that a key has a corresponding request number of 200, the first second is processed in 30 requests, and the remaining 170 requests are waiting to be queued.
        Assuming that a key corresponds to a request of 300, the portion exceeding 200 will return directly to 503.
.......
    } }

Reference: http://www.ttlsa.com/nginx/nginx-limiting-the-number-of-requests-ngx_http_limit_req_module-module/
Official document: Http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

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.