我需要更改哪些 httpd conf 设置才能增加 Apache 的最大并发连接数?注意:我关闭了 KeepAlive,因为这主要是一个 API 服务器。
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive Off
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15
##
## Server-Pool Size Regulation (MPM specific)
##
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>
# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
这里有关于MaxClients和MaxRequestsPerChild的计算的详细解释
ServerLimit 16
StartServers 2
MaxClients 200
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
首先,每当apache启动时,它都会启动2个子进程,这是由
StartServers
参数决定的。然后每个进程将启动由 ThreadsPerChild
参数确定的 25 个线程,因此这意味着 2 个进程只能服务 50 个并发连接/客户端,即 25x2=50。现在,如果出现更多并发用户,则会启动另一个子进程,该子进程可以为另外 25 个用户提供服务。但是可以启动多少个子进程是由ServerLimit
参数控制的,这意味着在上面的配置中,我总共可以有16个子进程,每个子进程可以处理25个线程,总共可以处理16x25=400个并发用户。但是,如果MaxClients
中定义的数量小于200,那么这意味着在8个子进程之后,不会启动额外的进程,因为我们已经定义了MaxClients
的上限。这也意味着,如果我将 MaxClients
设置为 1000,则在 16 个子进程和 400 个连接之后,不会启动额外的进程,即使我们增加了 MaxClient
参数,我们也无法为超过 400 个并发客户端提供服务。在这种情况下,我们还需要将 ServerLimit
增加到 1000/25,即 MaxClients/ThreadsPerChild=40
所以这是服务 1000 个客户端的优化配置
<IfModule mpm_worker_module>
ServerLimit 40
StartServers 2
MaxClients 1000
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
更改 MaxClients 指令。默认值通常是 256。
对于 Ubuntu 上的默认 Apache/PHP 安装,请将
mpm_worker_module
替换为 mpm_prefork_module
,如下所示:
<IfModule mpm_prefork_module>
ServerLimit 16
StartServers 2
MaxClients 200
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
</IfModule>
注意:如果您打算继续
mpm_worker_module
,您需要更改 PHP 多重处理模式。您可以按照此处描述的指南进行操作(未经测试):
https://askubuntu.com/a/1319874/496298