最近在研究web相关的东西,在window下的开发,默认都用了Tomcat作为server(除了spring boot里默认的)。联系到项目里的一个web application,当时是在linux平台下开发及部署的,所以用了Apache HTTP Server。 有同事就问起他俩到底什么关系。我把自己的认识以及从网上的了解翻译分享下。
关系
https://linuxacademy.com/community/posts/show/topic/30651-apache-httpd-vs-apache-tomcat
1. Apache HTTPD Server 与Apache Tomcat同属于Apache的开源项目。俩个都可以单独作为web server使用,但是又都有各自的特性。Apache HTTPD一般也就简称为Apache 了。
2. Apache是比较老的用来支撑静态web 项目的server。 而Tomcat主要是用来支撑动态web项目的,与数据库交互的系统。当然,Tomcat也可以用来开发静态web系统,只是性能上比着Httpd差些。
3 Apache 当然也可以支持动态web,因为从Apache的modules下,我们可以看到大量的modules以“mod_*.so”的命名。这些modules扩展了Apache的功能。从而可以让它能服务于采用PHP,CGI等server端开发语言。
4. Tomcat本身就是典型的Java Servlet引擎。它被用来作为Java Servlet的容器,管理Servlet的生命周期,从来处理动态页面请求等操作。
5. 最简练的一段话:
With server-side scripting (using CGI), Httpd can process Perl and PHP scripts (through loadable modules) in addition to HTML. Tomcat, on the other hand, also processes Java servlets and Java Server Pages (JSP) to create dynamic HTML that is sent to the browsers.
实际使用:
她们虽然在web系统上又各自的优势,但是他俩不是互斥的。可以结合使用。从网上找了个配置实例。
http://www.ntu.edu.sg/home/ehchua/programming/howto/apacheplustomcat_howto.html
Tomcat用来负责处理JSP或者java Servlet的,Apache httpd用来负责静态页面以及server-side functions such as CGI, PHP, SSI, etc
关于为什么如果又必要的话,最好是tomcat跟httpd配合使用,这个有个不错的解释:
https://wiki.apache.org/tomcat/FAQ/Connectors#Q3
Why should I integrate Apache with Tomcat? (or not)
There are many reasons to integrate Tomcat with Apache. And there are reasons why it should not be done too. Needless to say, everyone will disagree with the opinions here. With the performance of Tomcat 5 and 6, performance reasons become harder to justify. So here are the issues to discuss in integrating vs not.
- Clustering. By using Apache as a front end you can let Apache act as a front door to your content to multiple Tomcat instances. If one of your Tomcats fails, Apache ignores it and your Sysadmin can sleep through the night. This point could be ignored if you use a hardware loadbalancer and Tomcat's clustering capabilities.
- Clustering/Security. You can also use Apache as a front door to different Tomcats for different URL namespaces (/app1/, /app2/, /app3/, or virtual hosts). The Tomcats can then be each in a protected area and from a security point of view, you only need to worry about the Apache server. Essentially, Apache becomes a smart proxy server.
- Security. This topic can sway one either way. Java has the security manager while Apache has a larger mindshare and more tricks with respect to security. I won't go into this in more detail, but let Google be your friend. Depending on your scenario, one might be better than the other. But also keep in mind, if you run Apache with Tomcat - you have two systems to defend, not one.
- Add-ons. Adding on CGI, perl, PHP is very natural to Apache. Its slower and more of a kludge for Tomcat. Apache also has hundreds of modules that can be plugged in at will. Tomcat can have this ability, but the code hasn't been written yet.
- Decorators! With Apache in front of Tomcat, you can perform any number of decorators that Tomcat doesn't support or doesn't have the immediate code support. For example, mod_headers, mod_rewrite, and mod_alias could be written for Tomcat, but why reinvent the wheel when Apache has done it so well?
-
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
- Socket handling/system stability. Apache has better socket handling with respect to error conditions than Tomcat. The main reason is Tomcat must perform all its socket handling via the JVM which needs to be cross platform. The problem is socket optimization is a platform specific ordeal. Most of the time the java code is fine, but when you are also bombarded with dropped connections, invalid packets, invalid requests from invalid IP's, Apache does a better job at dropping these error conditions than JVM based program. (YMMV)
总而是从性能以及安全性来考虑的,当然要根据具体的应用场景来说。 Tomcat在形态页面处理上比Apache还是逊色的。而Tomcat对于动态页面的处理,以及java本身的security涉及,还是优于Apache的。
希望对大家是个参考。