配置 Apache / Tomcat 的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/105754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:44:38  来源:igfitidea点击:

Best practices for configuring Apache / Tomcat

javaapachetomcatjbossmod-proxy

提问by Jeremy

We are currently using Apache 2.2.3 and Tomcat 5 (Embedded in JBoss 4.2.2) using mod_proxy_jkas the connector.

我们目前使用 Apache 2.2.3 和 Tomcat 5(嵌入在 JBoss 4.2.2 中)mod_proxy_jk作为连接器。

Can someone shed some light on the the correct way to calculate / configure the values below (as well as anything else that may be relevant). Both Apache and Tomcat are running on separate machines and have copious amounts of ram (4gb each).

有人可以阐明计算/配置以下值的正确方法(以及其他任何可能相关的内容)。Apache 和 Tomcat 都在不同的机器上运行,并且有大量的 ram(每个 4gb)。

Relevant server.xml portions:

相关的 server.xml 部分:

<Connector port="8009"
    address="${jboss.bind.address}"
    protocol="AJP/1.3"
    emptySessionPath="true"
    enableLookups="false"
    redirectPort="8443"
    maxThreads="320"
    connectionTimeout="45000"
/>

Relevant httpd.conf portions:

相关的 httpd.conf 部分:

<IfModule prefork.c>
  StartServers       8
  MinSpareServers    5
  MaxSpareServers   20
  ServerLimit      256
  MaxClients       256
  MaxRequestsPerChild  0
</IfModule>

采纳答案by Zizzencs

You should consider the workload the servers might get.

您应该考虑服务器可能获得的工作负载。

The most important factor might be the number of simultaneously connected clients at peak times. Try to determine it and tune your settings in a way where:

最重要的因素可能是高峰时间同时连接的客户端数量。尝试确定它并以以下方式调整您的设置:

  • there are enough processing threads in both Apache and Tomcat that they don't need to spawn new threads when the server is heavily loaded
  • there are not way more processing threads in the servers than needed as they would waste resources.
  • Apache 和 Tomcat 中都有足够的处理线程,当服务器负载过重时,它们不需要产生新线程
  • 服务器中没有比需要更多的处理线程,因为它们会浪费资源。

With this kind of setup you can minimize the internal maintenance overhead of the servers, that could help a lot, especially when your load is sporadic.

通过这种设置,您可以最大限度地减少服务器的内部维护开销,这会很有帮助,尤其是当您的负载是零星的时。

For example consider an application where you have ~300 new requests/second. Each request requires on average 2.5 seconds to serve. It means that at any given time you have ~750 requests that need to be handled simultaneously. In this situation you probably want to tune your servers so that they have ~750 processing threads at startup and you might want to add something like ~1000 processing threads at maximum to handle extremely high loads.

例如,考虑一个每秒有大约 300 个新请求的应用程序。每个请求平均需要 2.5 秒来服务。这意味着在任何给定时间,您都有大约 750 个需要同时处理的请求。在这种情况下,您可能希望调整您的服务器,以便它们在启动时有大约 750 个处理线程,并且您可能希望最多添加大约 1000 个处理线程来处理极高的负载。

Also consider for exactly what do you require a thread for. In the previous example each request was independent from the others, there was no session tracking used. In a more "web-ish" scenario you might have users logged in to your website, and depending on your software used, Apache and/or Tomcat might need to use the same thread to serve the requests that come in one session. In this case, you might need more threads. However as I know Tomcat at least, you won't really need to consider this as it works with thread pools internally anyways.

还要考虑您到底需要线程做什么。在前面的示例中,每个请求都独立于其他请求,没有使用会话跟踪。在更“网络化”的场景中,您可能有用户登录到您的网站,并且根据您使用的软件,Apache 和/或 Tomcat 可能需要使用相同的线程来处理一个会话中出现的请求。在这种情况下,您可能需要更多线程。但是,至少我知道 Tomcat,您实际上不需要考虑这一点,因为它无论如何都可以在内部使用线程池。

回答by David Schmitt

MaxClients

最大客户数

This is the fundamental cap of parallel client connections your apache should handle at once.

这是您的 apache 应该立即处理的并行客户端连接的基本上限。

With prefork, only one request can be handled per process. Therefore the whole apache can process at most$MaxClients requests in the time it takes to handle a singlerequest. Of course, this ideal maximum can only be reached if the application needs less than 1/$MaxClients resources per request.

使用 prefork,每个进程只能处理一个请求。因此,处理单个请求所需的时间内,整个 apache 最多可以处理$MaxClients请求。当然,只有当应用程序每个请​​求需要的资源少于 1/$MaxClients 时,才能达到这个理想的最大值。

If, for example, the application takes a second of cpu-time to answer a single request, setting MaxClients to four will limit your throughput to four requests per second: Each request uses up an apache connection and apache will only handle four at a time. But if the server has only two CPUs, not even this can be reached, because every wall-clock second only has two cpu seconds, but the requests would need four cpu seconds.

例如,如果应用程序需要 1 秒的 CPU 时间来响应单个请求,则将 MaxClients 设置为 4 会将您的吞吐量限制为每秒 4 个请求:每个请求使用一个 apache 连接,而 apache 一次只能处理四个. 但是如果服务器只有两个 CPU,则甚至无法达到,因为每个挂钟秒只有两个 cpu 秒,而请求则需要 4 个 cpu 秒。

MinSpareServers

最小备用服务器

This tells apache how many idle processes should hang around. The bigger this number the more burst load apache can swallow before needing to spawn extra processes, which is expensive and thus slows down the current request.

这告诉 apache 应该挂多少空闲进程。这个数字越大,Apache 在需要产生额外的进程之前可以吞下更多的突发负载,这很昂贵,从而减慢了当前的请求。

The correct setting of this depends on your workload. If you have pages with many sub-requests (pictures, iframes, javascript, css) then hitting a single page might use up many more processes for a short time.

此项的正确设置取决于您的工作量。如果您的页面包含许多子请求(图片、iframe、javascript、css),那么点击单个页面可能会在短时间内消耗更多进程。

MaxSpareServers

最大备用服务器数

Having too many unused apache processes hanging around just wastes memory, thus apache uses the MaxSpareServers number to limit the amount of spare processes it is holding in reserve for bursts of requests.

有太多未使用的 apache 进程闲置只会浪费内存,因此 apache 使用 MaxSpareServers 数量来限制它为突发请求保留的备用进程数量。

MaxRequestsPerChild

最大请求数

This limits the number of requests a single process will handle throughout its lifetime. If you are very concerned about stability, you should put an actual limit here to continually recycle the apache processes to prevent resource leaks from affecting the system.

这限制了单个进程在其整个生命周期中将处理的请求数量。如果你很在意稳定性,你应该在这里设置一个实际的限制来不断回收apache进程,以防止资源泄漏影响系统。

StartServers

启动服务器

This is just the amount of processes apache starts by default. Set this to the usual amount of running apache processes to reduce warm-up time of your system. Even if you ignore this setting, apache will use the Min-/MaxSpareServers values to spawn new processes as required.

这只是 apache 默认启动的进程数量。将此设置为运行 apache 进程的通常数量,以减少系统的预热时间。即使您忽略此设置,apache 也会根据需要使用 Min-/MaxSpareServers 值来生成新进程。

More information

更多信息

See also the documentation for apache's multi-processing modules.

另请参阅apache 的多处理模块的文档

回答by f4nt

The default settings are generally decent starting points to see what your applications is really going to need. I don't know how much traffic you're expecting, so guessing at the MaxThreads, MaxClients, and MaxServers is a bit difficult. I can tell you that most of the customers I deal with (work for a linux web host, that deals mainly with customers running Java apps in Tomcat) use the default settings for quite some time without too many tweaks needed.

默认设置通常是不错的起点,可以查看您的应用程序真正需要什么。我不知道您期望多少流量,因此猜测 MaxThreads、MaxClients 和 MaxServers 有点困难。我可以告诉你,我处理的大多数客户(为 linux 网络主机工作,主要与在 Tomcat 中运行 Java 应用程序的客户打交道)使用默认设置很长一段时间,不需要太多调整。

If you're not expecting much traffic, then these settings being "too high" really shouldn't effect you too much either. Apache's not going to allocate resources for the whole 256 potential clients unless it becomes necessary. The same goes for Tomcat as well.

如果您不期望有太多流量,那么这些设置“太高”也不会对您产生太大影响。除非有必要,Apache 不会为全部 256 个潜在客户分配资源。Tomcat 也是如此。