Apache与Tomcat协同配置参考 下载本文

Apache与Tomcat协同配置参考

在按照本文档进行配置之前,假设Tomcat和Apache均已安装成功。

1. 优化tomcat与apache协同端口

通常情况下,tomcat的配置文件server.xml中并没有对apache与tomcat的连接池进行配置,建议按照如下方法配置:

maxThreads=\ minSpareThreads=\ maxSpareThreads=\ acceptCount=\

connectionTimeout=\ disableUploadTimeout=\

enableLookups=\ redirectPort=\ debug=\ protocol=\ />

主要参数意义:

minSpareThreads:最小空闲连接线程数,服务器启动时创建的处理请求的线程数,用于提高系统处理性能,默认值为10

maxSpareThreads:最大连接线程数,即:并发处理的最大请求数,默认值为75 acceptCount:允许的最大连接数,应大于等于maxProcessors,默认值为100 web server允许的最大连接数还受制于操作系统的内核参数设置,通常Windows是2000个左右,Linux是1000个左右,在Linux中可通过修改/etc/security/limits.conf 中的参数,增加如下配置

* soft nofile 163840 * hard nofile 163840

完整Tomcat配置server.xml文件参见附件server.xml。

2. 优化JVM内存

设置Tomcat的JVM的内存如下(在/tomcat/bin/catalina.sh或/tomcat/bin/catalina.bat中设置),

set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M –Xms512M

3. 正确配置Apache转发

特别注意,不要将Apache的转发策略(JkMount)配置成*,一个正确的Apache转发策略配置如下,

AddType text/html;charset=UTF-8 .js AddType text/html;charset=UTF-8 .css AddType text/html;charset=UTF-8 .html ServerName portal61.trs.cn ServerAdmin fu.wei@trs.com.cn JkMount /portal/*.do ajp13 JkMount /portal/*.jsp ajp13

JkMount /portal/stat/cewolf* ajp13 JkMount /portalfileupload/*.do ajp13 JkMount /portalfileupload/*.jsp ajp13

Alias /pub \Alias /web \Alias /webpic \Alias /preview \Alias /portaltemplate

DirectoryIndex loginpage.jsp

完整的Apache配置文件参见附件httpd.conf

\

4. 优化Apache线程和进程参数

Apache的缺省参数太小,可在Apache/conf/httpd.conf下添加如下配置,

ThreadLimit 128 ServerLimit 32 StartServers 5 MaxClients 4096 MinSpareThreads 125 MaxSpareThreads 256 ThreadsPerChild 128 MaxRequestsPerChild 5000 主要参数意义:

MaxClients 表示Apache最大连接数。应该大于等于ThreadsPerChild * ServerLimit; ThreadLimit表示打开的最大线程数,和ThreadsPerChild相关。如果ThreadsPerChild大于64,则需要同时调整ThreadLimit,并且ThreadLimit应该大于等于ThreadsPerChild;

ServerLimit表示打开的最大进程数,和StartServers相关。如果StartServers大于64,则需要同时调整ServerLimit,并且ServerLimit应该大于等于StartServers

5. 为静态文件增加Cache

在Apache/conf/httd.conf下增加如下配置,

LoadModule expires_module modules/mod_expires.so ExpiresActive on

ExpiresDefault \ExpiresByType image/gif A2592000

ExpiresByType application/x-javascript A1280000 ExpiresByType text/css A2000000

确认mod_expires.so加载一次便可,如原配置文件中已经加载过

mod_expires.so,则如上配置中的“LoadModule expires_module modules/mod_expires.so”此句可去掉。

6. 配置GZip压缩

采用deflate压缩模块,在Apache/conf/httd.conf增加如下配置,

LoadModule deflate_module modules/mod_deflate.so

#Insert filter

SetOutputFilter DEFLATE

# Netscape 4.x has some problems…

BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine # BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.59 # the above regex won’t work. You can use the following # workaround to get the desired effect:

# BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html force-gzip BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html

# Don’t compress images and other

SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary #SetEnvIfNoCase Remote_Addr

(192.9.200.*)|(192.9.100.*)|(218.247.176.131)$ no-gzip dont-vary AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css AddOutputFilterByType DEFLATE application/x-javascript # Make sure proxies don’t deliver the wrong content Header append Vary User-Agent env=!dont-vary

DeflateFilterNote ratio

LogFormat '\%h %l %u %t “%r\%>s %b \\-Agent}i\(%{ratio}n)' deflate

CustomLog logs/deflate_log deflate

确认mod_deflate.so加载一次便可,如原配置文件中已经加载过mod_deflate.so,则如上配置中的“LoadModule deflate_module modules/mod_deflate.so”此句可去掉。

完整Apache配置文件参见httpd.conf。

7. 验证GZip与Cache配置成功