1. 首先需要安装apache,安装步骤参见其官网,然后安装apache jk module
1.1.wgethttp://mirror.bjtu.edu.cn/apache//tomcat/tomcat-connectors/jk/source/jk-1.2.31/tomcat-connectors-1.2.31-src.tar.gz
1.2.tar-xzvftomcat-connectors-1.2.31-src.tar.gz
1.3.cdtomcat-connectors-1.2.31-src/native
1.4../configure--with-apxs=/usr/local/apache2/bin/apxs#/usr/local/apache2/是你apache的安装目录
1.5.make
1.6.cpapache-2.0/mod_jk.so/usr/local/apache2/modules/mod_jk.so,然后重新启动apache
1.7.进入apache安装目录,cd/usr/local/apache2/
2.配置mod_jk.conf
2.1.cdconf,vimod_jk.conf
2.2.编辑如下内容:
#globalconfig
#justconfigurelogandloadmodule
#loadmodule
LoadModulejk_modulemodules/mod_jk.so#表示导入jk模块
JkLogFilelogs/mod_jk.log#logs保存位置
JkLogStampFormat"[%a%b%d%H:%M:%S%Y]"#log时间戳格式
JkRequestLogFormat"%w%V%U%T"#log记录的内容
JkLogLevelwarn
JkWorkersFileconf/workers.properties#加载worker配置文件
#allvhostshouldinheritthisconfig
JkMountCopyAll#所有的虚拟主机都复制该配置文件
2.3JkRequestLogFormat参数说明:
%bBytessent,excludingHTTPheaders(CLFformat)
%BBytessent,excludingHTTPheaders
%HTherequestprotocol
%mTherequestmethod
%pThecanonicalPortoftheserverservingtherequest
%qThequerystring(prependedwitha?ifaquerystringexists,otherwiseanemptystring)
%rFirstlineofrequest
%sRequestHTTPstatuscode
%TRequestduration,elapsedtimetohandlerequestinseconds'.'microseconds
%UTheURLpathrequested,notincludinganyquerystring.
%vThecanonicalServerNameoftheserverservingtherequest
%VTheservernameaccordingtotheUseCanonicalNamesetting
%wTomcatworkername
%RSessionroutename(availablewith1.2.19andup)
3.配置workers.properties
viworkers.properties
#所有可以访问的工作机器,status用来监控jk的状态,一个工作机器可以理解为一个tomcat实例
worker.list=balancer,master,status
#type表示工作机器的类型,一般是type=ajp13和lb,lb表示负载平衡
worker.gmaster.port=8009
worker.master.host=10.3.1.22
worker.master.type=ajp13
worker.master.lbfactor=1#负载平衡因子,如果想要本机承载的负载大些,可以设置值大些
#definewebappslavehost
worker.slave.port=9009
worker.slave.host=localhost
worker.slave.type=ajp13
worker.slave.lbfactor=1
#definewebappbalancer
worker.balancer.type=lb#表示负载平衡
worker.balancer.balance_workers=master,lave#负载平衡工作机器,此列表的workers可以不放在worker.list
worker.balancer.sticky_session=True#有session的请求要指到原来tomcat上
#definestatus
worker.status.type=status#指明监控工作机器,可以不设置
4.配置uriworkermap.properties,它用来匹配哪些url被转发给tomcat
4.1.viuriworkermap.properties
#configurestatuspattern
/jkstatus=status
#configurebalancerpatterns
#justforword*.jsp,*.doandpageserverlet
/appname/*.jsp=balancer
/appname/*.do=balance
/appname/page=balance
/appname/*/page=balance
/appname/=balance
/appname=balance
#configureadminfunctionality,后台管理程序可以只匹配到一个工作机器,因为后台管理用户较少
/appname/admin/*.jsp=master
/appname/admin/*.do=master
/appname/admin/*.html=master
/appname/admin/page=master
/appname/admin/*/page=master
/appname/admin/=master
4.2.匹配优先级说明:
ThemostrestrictiveURIpatternisappliedfirst.MorepreciselytheURIpatternsaresortedby
thenumberof'/'charactersinthepattern(highestnumberfirst),andruleswithequalnumbers
aresortedbytheirstringlength(longestfirst).
Ifbothdistinctionsstilldonotsuffice,thenthedefiningsourceoftheruleisconsidered.
Rulesdefinedinuriworkermap.propertiescomefirst,beforerulesdefinedbyJkMount(Apache)
andinsideworkers.propertiesusingthemountattribute.
Alldisabledrulesareignored.Exclusionrulesareappliedafterallnormalruleshavebeenapplied.
Thereisnodefinedbehaviour,forthefollowingconfigurationconflict:usingliterallythesame
URIpatterninthesamedefiningsourcebutwithdifferentworkertargets.
5.配置httpd.conf
vihttpd.conf
在最后面加载Includeconf/mod_jk.conf
6.配置httpd-vhosts.conf
viextra/httpd-vhosts.conf
<VirtualHost*:80>
ServerNameyourweb.com
DocumentRoot"/usr/local/apache2/yourdocroot"
#放置Alias块
RewriteEngineon
RewriteRule^/logging.php*yourapp/admin/[PT]
JkMountFileconf/uriworkermap.properties#加载配置规则,仅对本虚拟主机生效
</VirtualHost>
PT表示passthrough,直接发个apache内部
R表示redirect
RewriteRule处理快于jk匹配,Rewrite规则可以参见apache官网,它像一个钩子,可以修改很多行为
7.使用apacheAlias实现apache静态文件转发功能,即静态文件请求tomcat,直接通过apache提供服务
把这一段放在<VirtualHost*:80></VirtualHost>之间,一般放在DocumentRoot的下一行即可
Alias/yourapp/js/var/yourapp_static/js/
<Directory"/var/yourapp_static/js">
OptionsIndexesMultiViews
AllowOverrideNone
Orderallow,deny
Allowfromall
</Directory>
你也可以放置多个这样的别名块
8.配置tomcat
8.1.配置jvm内存,vibin/catalina.sh,在最前面添加:JAVA_OPTS='-Xms1000m-Xmx1000m'
8.2.配置ajpconnector,在server.xml中配置
<Connectorprotocol="AJP/1.3"port="0"
channelNioSocket.port="8009"
channelNioSocket.redirectPort="8443"
channelNioSocket.maxThreads="256"
channelNioSocket.maxSpareThreads="97"
channelNioSocket.minSpareThreads="50"
channelNioSocket.URIEncoding="UTF-8"
URIEncoding="UTF-8"
channelNioSocket.connectionTimeout="20000"
channelNioSocket.keepAliveTimeout="1000"
channelNioSocket.bufferSize="16384"/>
使用了nio
8.3.生产环境中你可以关掉httpconnector
9.配置操作系统文件描述符数量
vi/etc/profile
加入ulimit-SHn51200
后保存
source/etc/profile
#一般系统的文件描述符是1024,你可以使用ulimit-a查看,当有大量tcp连接时,文件描述符可能不够用
参数说明:
-H设置硬资源限制,一旦设置不能增加。
-S设置软资源限制,设置后可以增加,但是不能超过硬资源设置。
-a显示当前所有的limit信息。
-c最大的core文件的大小,以blocks为单位。
-d进程最大的数据段的大小,以Kbytes为单位。
-f进程可以创建文件的最大值,以blocks为单位。
-l最大可加锁内存大小,以Kbytes为单位。
-m最大内存大小,以Kbytes为单位。
-n可以打开最大文件描述符的数量。
-p管道缓冲区的大小,以Kbytes为单位。
-s线程栈大小,以Kbytes为单位。
-t最大的CPU占用时间,以秒为单位。
-u用户最大可用的进程数。
-v进程最大可用的虚拟内存,以Kbytes为单位。