如何在Apache中增加最大并发连接数?

111

我需要更改哪些httpd conf设置来增加Apache的最大并发连接数?注意:由于这主要是一个API服务器,因此我关闭了KeepAlive。

#
# 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>
2个回答

186

这里是有关计算MaxClients和MaxRequestsPerChild的详细解释。

http://web.archive.org/web/20160415001028/http://www.genericarticles.com/mediawiki/index.php?title=How_to_optimize_apache_web_server_for_maximum_concurrent_connections_or_increase_max_clients_in_apache

ServerLimit 16
StartServers 2
MaxClients 200
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25

首先,无论何时启动Apache服务器,它将启动由StartServers参数确定的2个子进程。然后,每个进程将启动由ThreadsPerChild参数确定的25个线程,这意味着2个进程只能服务50个并发连接/客户端,即25x2=50。如果有更多并发用户,则会启动另一个子进程,可以为另外25个用户提供服务。但可以启动多少个子进程由ServerLimit参数控制。在上述配置中,这意味着我最多可以拥有16个子进程,每个子进程可以处理25个线程,总共可以处理16x25=400个并发用户。但是,如果MaxClients中定义的数字较小,在这里是200,则意味着在8个子进程之后,不会启动额外的进程,因为我们定义了MaxClients的上限。这也意味着,即使我们增加了MaxClient参数,如果在16个子进程和400个连接后,将不能服务超过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>

26
2.3.13版本之后似乎有一些变化。例如,MaxClients现在改为MaxRequestWorkers。 - ılǝ
2
请注意:目前链接的网站正在提供恶意软件(和色情内容)...它可能已经被黑客攻击...如果你在工作时在Stack Overflow上搜索解决方案,一个完整的色情网站会弹出来,这是非常让人烦恼的...https://sitecheck.sucuri.net/results/www.genericarticles.com - yoano
1
好的,但是这个最佳配置需要多少内存和CPU呢?我怎么考虑CPU和内存对于这个优化的影响呢? - indianwebdevil
我应用了这个配置,但仍然可以访问当前的连接...似乎是其他地方有一个硬限制。 - Jorge Cornejo Bellido
这个链接 https://www.jeffgeerling.com/blog/3-small-tweaks-make-apache-fly 可以帮助你在设置指令以达到可接受的结果时进行微调。尽管如此,我自己还在从这篇文章中学习。 - Lex Soft

11

修改MaxClients指令。默认值通常为256。


2
这里是文档链接:http://httpd.apache.org/docs/1.3/mod/core.html#maxclients - NullUserException

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接