简体   繁体   English

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

[英]Apache: how to setup in Apache/mod_proxy an upper bound on the number of concurrent forwarded requests?

I have a fairly standard setup where a front-end Apache server forwards requests to Tomcat through mod_proxy/AJP. 我有一个相当标准的设置,其中前端Apache服务器通过mod_proxy / AJP将请求转发到Tomcat。 How can I setup Apache/mod_proxy so it only forwards at most N (say, N=4) concurrent requests to Tomcat? 如何设置Apache / mod_proxy,使其最多仅转发N个(例如N = 4)并发请求到Tomcat? Other concurrent requests coming into Apache should not be rejected, and should instead be queued to later be sent to Tomcat. 进入Apache的其他并发请求不应被拒绝,而应该排队等待稍后发送到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 1:请注意,您可以使用maxThreads属性在Tomcat级别执行此操作,但是我更喜欢在Apache级别进行处理。

PS 2: I see that Apache has a MaxClients configuration, which seems to be doing what I am looking for. PS 2:我看到Apache具有MaxClients配置,这似乎可以满足我的需求。 But it is not clear to me how to have a MaxClient per server mod_proxy forwards to, rather than MaxClient per Apache. 但是我不清楚如何将每台服务器的MaxClient传递给mod_proxy,而不是每个Apache的MaxClient。 Ie 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). 即,如果Apache将请求转发到4个Tomcat计算机的集群,我希望Apache将转发给任何给定Tomcat的并发请求数限制为N(例如,N = 4)。

The solutions is mod_proxy by adding parameters to ProxyPass directives. 通过向ProxyPass指令添加参数,解决方案是mod_proxy。 What you want to set is probably the max . 您想要设置的可能是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. 如果您确实要排队,则还必须使用mod_proxy_balancer。 For example allow maximum 4 connections: 例如,最多允许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> 

Unfortunately, with in Apache, the value of max is per process. 不幸的是,在Apache中, max的值是每个进程的。 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: 因此,仅当Apache有一个进程并使用线程而不是进程来处理多个连接时,才可以有效地限制与后端服务器的连接数,这取决于Apache使用的MPM

  • 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. 在Windows上 ,您应该一切都很好,并且很可能不必担心这一点,因为Winnt MPM使用一个进程,该进程进而创建线程来处理请求。
  • 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: 在UNIX上 ,如果您使用的是操作系统随附的Apache,那么很可能您有预分支MPM Apache,它会为每个请求创建一个进程,并且max参数无法使用:

    1. To check what MPM you have, run apachectl -l . 要检查您拥有的MPM,请运行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. 在列表中,如果看到worker.cevent.c ,那么您几乎可以做到:现在只需要确保Apache仅创建一个进程即可。 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. 为此,请将ThreadsPerChildMaxClients设置为相同的值,这将是Apache能够处理的并发连接总数。 Also set ServerLimit to 1. ServerLimit设置为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. 在列表中,如果看到prefork.c ,则首先需要用工作程序或事件MPM Apache替换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. 您可以自己重新编译Apache(MPM不是运行时配置参数),也可以为平台获取现有的软件包。 Then, go to step two. 然后,转到第二步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM