Table of Contents
PHP没有数据库连接池怎么破?PHP环境下使用Nginx ngx_http_limit_req_module模块的高负载解决方案
Home php教程 php手册 PHP没有数据库连接池怎么破?PHP环境下使用Nginx ngx_http_limit_req_module模块的高负载解决方案

PHP没有数据库连接池怎么破?PHP环境下使用Nginx ngx_http_limit_req_module模块的高负载解决方案

Jun 13, 2016 am 08:58 AM
module environment solution

PHP没有数据库连接池怎么破?PHP环境下使用Nginx ngx_http_limit_req_module模块的高负载解决方案

线上运行了一套辅助系统是利用了开源的PHP改的,之前也没怎么玩过PHP,没想到这玩意儿还是有不少坑的。突然某一天一个用户做线上活动推广,然后短时间内涌进来了上万的请求,然后数据库连接耗尽,短时间内几乎拖垮了整个系统。导致系统奔溃的有多方面原因,今天主要针对PHP没有数据库连接池的原因来分析。

在PHP里,数据库连接在请求到达时建立,请求结束时释放。如果同时几千个请求到达,那就同时会建立几千个数据库连接,非常恐怖。而且PHP木有比较好的数据库连接池驱动方案,所以我们得另想办法。

解决这种问题有三种办法:
1.使用Mysql Proxy中间件。Mysql Proxy提供了连接池管理的功能。但是我们没有采用此方法,因为情况紧急,没有人熟悉这玩意儿。
2.使用PHP-FPM。PHP-FPM是PHP的一个FastCGI进程管理器。通过配置可以控制同时处理PHP请求的进程数。
具体可以参考:http://www.linuxde.net/2013/06/14638.html
但是我们也没用使用此方案,因为安装配置过程比较麻烦。
3.使用Nginx的ngx_http_limit_req_module来控制请求。
此模块可以通过自定义的键值来限制请求频率。限制的方法就像漏斗,每秒固定处理请求数,然后推迟超出的请求,最后超出最大值的直接503返回拒绝。
我们使用了此方案,是因为只需简单配置,而且可以灵活控制限制请求的场景。例如,对于静态资源的请求我们不做限制,而对于PHP的请求做限制。还可以从URL地址里提取出变量信息作为键,来达到更细的请求限制。
下面贴部分我们的配置给大家讲解下。

<code bash="" class="hljs">http {
    ......
    limit_req_zone $limit_key zone=limit_one:50m rate=30r/s;
    #定义limit_key为Key的变量名,用于后面赋值,每个Key都有自己的计数器。limit_one为zone的名称。rate表示每秒最多接受30个同时请求。
    server {
        ......
        if ( $request_uri ~* .*php.* ) {
              set $mp_limit_key $binary_remote_addr;
              #对于全部PHP首先有个默认的Key,使用客户端的IP作为Key。相当于每个客户端IP都会在zone的限制内。
         }
        if ( $query_string ~* .*id/(d+).php.* ) {
              set $mp_limit_key $1;
              #提取id后面的值作为Key。
        }
        if ( $query_string ~* .*appid/wx(.*).html.* ) {
              set $mp_limit_key $1;
              #提取appid作为Key。
        }
        limit_req zone=limit_one burst=200;
        #限制limit_one在此server内的漏斗容量为200。假设一个Key对应的请求数为200,那么第一秒内在处理的为30个请求,其余的170个请求在等待排队。假设一个Key对应的请求数为300,那么超出200的部分将直接返回503。
        .......
    }
}</code>
Copy after login

 

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)

Solution for Win11 unable to install Chinese language pack Solution for Win11 unable to install Chinese language pack Mar 09, 2024 am 09:15 AM

Win11 is the latest operating system launched by Microsoft. Compared with previous versions, Win11 has greatly improved the interface design and user experience. However, some users reported that they encountered the problem of being unable to install the Chinese language pack after installing Win11, which caused trouble for them to use Chinese in the system. This article will provide some solutions to the problem that Win11 cannot install the Chinese language pack to help users use Chinese smoothly. First, we need to understand why the Chinese language pack cannot be installed. Generally speaking, Win11

Unable to boot into Windows recovery environment Unable to boot into Windows recovery environment Feb 19, 2024 pm 11:12 PM

Windows Recovery Environment (WinRE) is an environment used to repair Windows operating system errors. After entering WinRE, you can perform system restore, factory reset, uninstall updates, etc. If you are unable to boot into WinRE, this article will guide you through fixes to resolve the issue. Unable to boot into the Windows Recovery Environment If you cannot boot into the Windows Recovery Environment, use the fixes provided below: Check the status of the Windows Recovery Environment Use other methods to enter the Windows Recovery Environment Did you accidentally delete the Windows Recovery Partition? Perform an in-place upgrade or clean installation of Windows below, we have explained all these fixes in detail. 1] Check Wi

Reasons and solutions for scipy library installation failure Reasons and solutions for scipy library installation failure Feb 22, 2024 pm 06:27 PM

Reasons and solutions for scipy library installation failure, specific code examples are required When performing scientific calculations in Python, scipy is a very commonly used library, which provides many functions for numerical calculations, optimization, statistics, and signal processing. However, when installing the scipy library, sometimes you encounter some problems, causing the installation to fail. This article will explore the main reasons why scipy library installation fails and provide corresponding solutions. Installation of dependent packages failed. The scipy library depends on some other Python libraries, such as nu.

An effective solution to solve the problem of garbled characters caused by Oracle character set modification An effective solution to solve the problem of garbled characters caused by Oracle character set modification Mar 03, 2024 am 09:57 AM

Title: An effective solution to solve the problem of garbled characters caused by Oracle character set modification. In Oracle database, when the character set is modified, the problem of garbled characters often occurs due to the presence of incompatible characters in the data. In order to solve this problem, we need to adopt some effective solutions. This article will introduce some specific solutions and code examples to solve the problem of garbled characters caused by Oracle character set modification. 1. Export data and reset the character set. First, we can export the data in the database by using the expdp command.

Oracle NVL function common problems and solutions Oracle NVL function common problems and solutions Mar 10, 2024 am 08:42 AM

Common problems and solutions for OracleNVL function Oracle database is a widely used relational database system, and it is often necessary to deal with null values ​​during data processing. In order to deal with the problems caused by null values, Oracle provides the NVL function to handle null values. This article will introduce common problems and solutions of NVL functions, and provide specific code examples. Question 1: Improper usage of NVL function. The basic syntax of NVL function is: NVL(expr1,default_value).

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Jun 03, 2024 pm 01:25 PM

Common challenges faced by machine learning algorithms in C++ include memory management, multi-threading, performance optimization, and maintainability. Solutions include using smart pointers, modern threading libraries, SIMD instructions and third-party libraries, as well as following coding style guidelines and using automation tools. Practical cases show how to use the Eigen library to implement linear regression algorithms, effectively manage memory and use high-performance matrix operations.

Revealing the method to solve PyCharm key failure Revealing the method to solve PyCharm key failure Feb 23, 2024 pm 10:51 PM

PyCharm is a powerful Python integrated development environment that is widely loved by developers. However, sometimes we may encounter key invalidation problems when using PyCharm, resulting in the inability to use the software normally. This article will reveal the solution to PyCharm key failure and provide specific code examples to help readers quickly solve this problem. Before we start solving the problem, we first need to understand why the key is invalid. PyCharm key failure is usually due to network problems or the software itself

Resolve Unable to start application properly error code 0xc000007b Resolve Unable to start application properly error code 0xc000007b Feb 20, 2024 pm 01:24 PM

How to solve the problem of unable to start normally 0xc000007b When using the computer, we sometimes encounter various error codes, one of the most common is 0xc000007b. When we try to run some applications or games, this error code suddenly appears and prevents us from starting it properly. So, how should we solve this problem? First, we need to understand the meaning of error code 0xc000007b. This error code usually indicates that one or more critical system files or library files are missing, corrupted, or incorrect.

See all articles