Linux 在 web 服务器中提供静态内容和在 tomcat 中提供动态内容仍然是一个很好的性能实践吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677108/
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-08-03 23:15:41  来源:igfitidea点击:

Serve static content in a web server and dynamic content in tomcat is still a good performance practice?

javaperformancearchitecturetomcatapache

提问by Lucas Machado

In old versions of tomcat (like 3.2) you can find the recomendation to serve static content in a apache web server and leave the dynamic content to the tomcat itself. At the new versions of the tomcat docs you cannot find any reference to this practice even at the mod_jk configuration tutorial. So I am wondering.

在旧版本的 tomcat(如 3.2)中,您可以找到在 apache web 服务器中提供静态内容并将动态内容留给 tomcat 本身的建议。在新版本的 tomcat 文档中,即使在 mod_jk 配置教程中也找不到任何对此实践的参考。所以我想知道。

Is still true that tomcat is not good enough to serve static content? If I change my deploy schema to separate static and dynamic content I will have performance improvement?

仍然认为 tomcat 不足以提供静态内容吗?如果我更改我的部署架构以分离静态和动态内容,我的性能会有所提高吗?

回答by Colin Hebert

You shouldn't consider speed when you choose between Apache httpd and Tomcat.

在 Apache httpd 和 Tomcat 之间进行选择时,您不应该考虑速度。

What is JK (or AJP)?

AJP is a wire protocol. It an optimized version of the HTTP protocol to allow a standalone web server such as Apache to talk to Tomcat. Historically, Apache has been much faster than Tomcat at serving static content. The idea is to let Apache serve the static content when possible, but proxy the request to Tomcat for Tomcat related content.

什么是 JK(或 AJP)?

AJP 是一种有线协议。它是 HTTP 协议的优化版本,允许独立的 Web 服务器(如 Apache)与 Tomcat 对话。从历史上看,Apache 在提供静态内容方面比 Tomcat 快得多。这个想法是在可能的情况下让Apache服务静态内容,但将请求代理到Tomcat以获取Tomcat相关内容。

And

Speed. Apache is faster at serving static content than Tomcat. But unless you have a high traffic site, this point is useless. But in some scenarios, tomcat can be faster than Apache httpd. So benchmark YOUR site. Tomcat can perform at httpd speeds when using the proper connector (APR with sendFile enabled). Speed should not be considered a factor when choosing between Apache httpd and Tomcat

速度。Apache 在提供静态内容方面比 Tomcat 更快。但是除非你有一个高流量的网站,这点是没有用的。但是在某些场景下,tomcat 可以比 Apache httpd 更快。因此,对您的网站进行基准测试。使用正确的连接器(启用了 sendFile 的 APR)时,Tomcat 可以以 httpd 速度执行。在 Apache httpd 和 Tomcat 之间进行选择时,不应将速度视为一个因素



Resources :

资源 :

回答by Pascal Thivent

Regarding pure speed, I suggest to read the Myth or truth: One should always use Apache httpd in front of Apache Tomcat to improve performance?blog post. Let me quote it partially:

关于纯粹的速度,我建议阅读神话或真相:应该始终在 Apache Tomcat 前面使用 Apache httpd 以提高性能?博客文章。让我部分引用:

The short answer is that this is a myth. The longer answer is that back in the days of Tomcat 3 there was some truth to this depending on circumstances. However, for the versions of Tomcat in use today (5.5.x and 6.0.x) then there is no need to use httpd for purely performance reasons. Tomcat now supports the native/APR connector which uses the same native library (the Apache Portable Runtime—APR) as httpd for the low-level I/O and therefore can achieve similar performance to httpd. When serving static content there is ever so slightly more overhead when using Tomcat compared to httpd but the differences are so small they are unlikely to be noticeable in production systems.

...

The performance testingperformed by Christopher Schultz, a regular on the Tomcat users mailing list, used a wider range of file sizes and provides – in my view – better results. The results of his tests are shown in the graph below.

alt text

These results are much more in line with what is expected, although there are a few interesting points to note:

  • Apache httpd and Coyote APR/native show similar performance levels.
  • Coyote NIO isn't too far behind httpd and Coyote APR/native.
  • There appears to be a limit on the usefulness of sendfile. This may be a hardware limitation but is worthy of further attention. I've added this to my todo list.
  • For small file sizes (less than ~10KiB) the static file caching in Tomcat provides a significant performance boost.

...

While raw performance for static content may not be a good reason to use httpd, there are a number of good reasons why you might want to use httpd with Tomcat. The most frequent reason is to provide load-balancing to two or more Tomcat instances. httpd isn't the only option to so this - hardware load balancers or other reverse proxies can be used - but it is a popular choice amongst system administrators as many of them are already familiar with httpd. I'll write more on using httpd as a load-balancer in a future article.

...

简短的回答是,这是一个神话。更长的答案是,在 Tomcat 3 的时代,这取决于具体情况。但是,对于今天使用的 Tomcat 版本(5.5.x 和 6.0.x),纯粹出于性能原因,没有必要使用 httpd。Tomcat 现在支持 native/APR 连接器,它使用与 httpd 相同的本机库(Apache Portable Runtime - APR)用于低级 I/O,因此可以获得与 httpd 相似的性能。在提供静态内容时,与 httpd 相比,使用 Tomcat 时的开销要稍微多一些,但差异很小,在生产系统中不太可能引起注意。

...

Tomcat 用户邮件列表上的常客 Christopher Schultz 执行的性能测试使用了更广泛的文件大小,并在我看来提供了更好的结果。他的测试结果如下图所示。

替代文字

这些结果更符合预期,尽管有一些有趣的点需要注意:

  • Apache httpd 和 Coyote APR/native 表现出相似的性能水平。
  • Coyote NIO 并不落后于 httpd 和 Coyote APR/native。
  • sendfile 的用处似乎是有限的。这可能是硬件限制,但值得进一步关注。我已将此添加到我的待办事项列表中。
  • 对于小文件(小于 ~10KiB),Tomcat 中的静态文件缓存提供了显着的性能提升。

...

虽然静态内容的原始性能可能不是使用 httpd 的一个很好的理由,但您可能希望将 httpd 与 Tomcat 一起使用有很多很好的理由。最常见的原因是为两个或多个 Tomcat 实例提供负载平衡。httpd 不是唯一的选择 - 可以使用硬件负载平衡器或其他反向代理 - 但它是系统管理员中的流行选择,因为他们中的许多人已经熟悉 httpd。我将在以后的文章中详细介绍如何使用 httpd 作为负载平衡器。

...

Well worth the read.

非常值得一读。

That being said, using a web server in front of Tomcat to serve static content will obviously free up more power for dynamic content and is thus my favorite option.

话虽如此,在 Tomcat 前面使用 Web 服务器来提供静态内容显然可以为动态内容释放更多的权力,因此是我最喜欢的选择。

回答by evernat

Perhaps a Cache-Control header with a max-age value can be added by a servlet filter in Tomcat for all "/static/*" content. And so if the same clients come back often (Intranet), Tomcat could be enough since static content will be in browsers cache and rarely asked to the server compared to dynamic content.

也许 Tomcat 中的 servlet 过滤器可以为所有“/static/*”内容添加具有 max-age 值的 Cache-Control 标头。因此,如果相同的客户端经常返回(内联网),Tomcat 可能就足够了,因为与动态内容相比,静态内容将在浏览器缓存中并且很少被请求到服务器。

Moreover Cache-Control for static content could be a good idea for an extended network bandwidth.

此外,静态内容的缓存控制对于扩展网络带宽可能是一个好主意。

(It's just my own experience striving to KISS)

(这只是我自己努力KISS的经历)