Apache:如何在 Apache/mod_proxy 中设置并发转发请求数的上限?
我有一个相当标准的设置,其中前端 Apache 服务器通过 mod_proxy/AJP 将请求转发到 Tomcat。如何设置 Apache/mod_proxy 使其最多仅将 N 个(例如 N=4)个并发请求转发到 Tomcat?进入 Apache 的其他并发请求不应被拒绝,而应排队等待稍后发送到 Tomcat。
PS 1:请注意,您可以使用 maxThreads 属性在 Tomcat 级别执行此操作,但我更喜欢在 Apache 级别处理此操作。
PS 2:我看到Apache有一个 MaxClients 配置,这似乎正在做我正在寻找的事情。但我不清楚如何让 mod_proxy 转发到每个服务器的 MaxClient,而不是每个 Apache 的 MaxClient。即,如果 Apache 将请求转发到由 4 台 Tomcat 机器组成的集群,我希望 Apache 将转发到任何给定 Tomcat 的并发请求数限制为 N(例如,N=4)。
I have a fairly standard setup where a front-end Apache server forwards requests to Tomcat through mod_proxy/AJP. How can I setup Apache/mod_proxy so it only forwards at most N (say, N=4) concurrent requests to Tomcat? Other concurrent requests coming into Apache should not be rejected, and should instead be queued to later be sent to Tomcat.
PS 1: Note that this is something you can do this at Tomcat level with the maxThreads
attribute, but I prefer to handle this at the Apache level.
PS 2: I see that Apache has a MaxClients configuration, which seems to be doing what I am looking for. But it is not clear to me how to have a MaxClient per server mod_proxy forwards to, rather than MaxClient per Apache. I.e. if Apache forward requests to a cluster of 4 Tomcat machine, I'd like Apache to limit the number of concurrent requests forwarded to any given Tomcat to N (say, N=4).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是通过向 ProxyPass 指令添加参数来 mod_proxy。您想要设置的可能是最大值。然而,这会立即引发错误,并且当您达到最大值时不会对您的请求进行排队。
如果你真的想排队,你还必须使用 mod_proxy_balancer。例如,最多允许 4 个连接:
不幸的是,在 Apache 中,
max
的值是每个进程的。因此,只有当 Apache 有一个进程并使用线程而不是进程来处理多个连接时,您才能有效地限制与后端服务器的连接数量,这取决于什么。 org/docs/2.2/mpm.html" rel="nofollow">MPM 正在被 Apache 使用:在 UNIX 上,如果您使用操作系统附带的 Apache,不幸的是您很有可能拥有 prefork MPM Apache,它为每个请求创建一个进程,并且 max 参数将不起作用:
apachectl -l
。worker.c
或event.c
,那么您就差不多好了:您现在只需要确保 Apache 仅创建一个过程。为此,请将 ThreadsPerChild 和 MaxClients 设置为相同的值,这将是 Apache 能够处理的并发连接总数。同时将ServerLimit
设置为 1。prefork.c
,那么您首先需要将 Apache 替换为工作器或事件 MPM Apache。您可以通过自己重新编译 Apache(MPM 不是运行时配置参数)或获取适合您平台的现有包来实现此目的。然后,转到第二步。The solutions is mod_proxy by adding parameters to ProxyPass directives. What you want to set is probably the max. This however will throw an error instantly and not queue your requests when you hit the max.
If you really want to queue you have to use also mod_proxy_balancer. For example allow maximum 4 connections:
Unfortunately, with in Apache, the value of
max
is per process. So, you can only effectively limit the number of connections to your back-end servers if Apache has one process and uses threads instead of processes to handle multiple connections, which depends on what MPM is being used by Apache:On UNIX, if you're using the Apache that came with your OS, unfortunately there is a good chance you have prefork MPM Apache, which creates one process per request, and with which the max parameter wouldn't work:
apachectl -l
.worker.c
orevent.c
, then you are almost good: you now just need to make sure that Apache creates only one process. For this, setThreadsPerChild
andMaxClients
to the same value, which will be the total number of concurrent connections your Apache will be able to process. Also setServerLimit
to 1.prefork.c
, then you first need to replace your Apache with the worker or event MPM Apache. You can do so by either recompiling Apache yourself (the MPM is not a run-time configuration parameter), or getting a existing package for your platform. Then, go to step two.