Apache:如何在 Apache/mod_proxy 中设置并发转发请求数的上限?

发布于 2024-09-08 08:21:49 字数 551 浏览 10 评论 0原文

我有一个相当标准的设置,其中前端 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞别 2024-09-15 08:21:49

解决方案是通过向 ProxyPass 指令添加参数来 mod_proxy。您想要设置的可能是最大值。然而,这会立即引发错误,并且当您达到最大值时不会对您的请求进行排队。

如果你真的想排队,你还必须使用 mod_proxy_balancer。例如,最多允许 4 个连接:

ProxyPass / balancer://appservers/ stickysession=JSESSIONID|jsessionid nofailover=On
<Proxy balancer://appservers>
    BalancerMember ajp://192.168.0.100:8009 max=4
    BalancerMember ajp://192.168.0.101:8009 max=4
    BalancerMember ajp://192.168.0.102:8009 max=4
    BalancerMember ajp://192.168.0.103:8009 max=4
</Proxy> 

不幸的是,在 Apache 中,max 的值是每个进程的。因此,只有当 Apache 有一个进程并使用线程而不是进程来处理多个连接时,您才能有效地限制与后端服务器的连接数量,这取决于什么。 org/docs/2.2/mpm.html" rel="nofollow">MPM 正在被 Apache 使用:

  • 在 Windows 上,你应该一切都好,而且很可能不必这样做担心这一点,因为 winnt MPM 使用一个进程,该进程反过来创建线程来处理请求。
  • 在 UNIX 上,如果您使用操作系统附带的 Apache,不幸的是您很有可能拥有 prefork MPM Apache,它为每个请求创建一个进程,并且 max 参数将不起作用:

    1. 要检查您拥有的 MPM,请运行 apachectl -l
    2. 在列表中,如果您看到 worker.cevent.c,那么您就差不多好了:您现在只需要确保 Apache 仅创建一个过程。为此,请将 ThreadsPerChild 和 MaxClients 设置为相同的值,这将是 Apache 能够处理的并发连接总数。同时将 ServerLimit 设置为 1。
    3. 在列表中,如果您看到 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:

ProxyPass / balancer://appservers/ stickysession=JSESSIONID|jsessionid nofailover=On
<Proxy balancer://appservers>
    BalancerMember ajp://192.168.0.100:8009 max=4
    BalancerMember ajp://192.168.0.101:8009 max=4
    BalancerMember ajp://192.168.0.102:8009 max=4
    BalancerMember ajp://192.168.0.103:8009 max=4
</Proxy> 

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 Windows, you should be all good and most likely don't have to worry about this, as the winnt MPM uses one process which in turn creates threads to handle requests.
  • 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:

    1. To check what MPM you have, run apachectl -l.
    2. In the list, if you see worker.c or event.c, then you are almost good: you now just need to make sure that Apache creates only one process. For this, set ThreadsPerChild and MaxClients to the same value, which will be the total number of concurrent connections your Apache will be able to process. Also set ServerLimit to 1.
    3. In the list, if you see 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文