主程序:
·tomcat·tomcat-admin-webapps·tomcat-webapps·tomcat-docs-webapp·java-1.8.0-openjdk
配置文件:
配置文件目录:/etc/tomcat主配置文件:server.xmlwebapps存放位置:/var/lib/tomcat/webapps/webapps的根目录·index.jsp:主页;·WEB-INF/:当前webapp的私有资源路径;通常用于存储当前webapp的web.xml和context.xml配置文件;·META-INF/:类似于WEB-INF/;·classes/:类文件,当前webapp所提供的类;·lib/:类文件,当前webapp所提供的类,被打包为jar格式环境配置文件:/etc/sysconfig/tomcat·server.xml:主配置文件;·web.xml:每个webapp只有“部署”后才能被访问,它的部署方式通常由web.xml进行定义,其存放位置为WEB-INF/目录中;·context.xml:每个web都可以专用的配置文件,它通常由专用的配置文件context.xml来定义,其存放位置为WEB-INF/目录中;·tomcat-users.xml:用户认证的账号和密码文件;·catalina.policy:当使用-security选项启动tomcat时,用于为tomcat设置安全策略;·catalina.properties:Java属性的定义文件,用于设定类加载器路径,以及一些与JVM调优相关参数;·logging.properties:日志系统相关的配置
测试类代码:
#hello world<%@ page language=”java” %><%@ page import=”java.util.*” %><html><head><title>Test Page</title></head><body><% out.println(“hello world”);%></body></html>#session会话测试<%@ page language=”java” %><html><head><title>TomcatB</title></head><body><h1><font color=”blue”>TomcatB.magedu.com</font></h1><table align=”centre” border=”1″><tr><td>Session ID</td><% session.setAttribute(“magedu.com”,”magedu.com”); %><td><%= session.getId() %></td></tr><tr><td>Created on</td><td><%= session.getCreationTime() %></td></tr></table></body></html>
常用组件配置:
·Server:代表tomcat instance,即表现出的一个java进程;监听在8005端口,只接收“SHUTDOWN”。各server监听的端口不能相同。
·Service:用于实现将一个或多个connector组件关联至一个engine组件;
·Connector:
负责接收请求,常见的有三类http/https/ajp;进入tomcat的请求可分为两类:(1) standalone : 请求来自于客户端浏览器;(2) 由其它的web server反代:来自前端的反代服务器;nginx –> http connector –> tomcathttpd(proxy_http_module) –> http connector –> tomcathttpd(proxy_ajp_module) –> ajp connector –> tomcat属性:port=”8080″protocol=”HTTP/1.1″connectionTimeout=”20000″address:监听的IP地址;默认为本机所有可用地址;maxThreads:最大并发连接数,默认为200;enableLookups:是否启用DNS查询功能;acceptCount:等待队列的最大长度;
·Engine组件:Servlet实例,即servlet引擎,其内部可以一个或多个host组件来定义站点; 通常需要通过defaultHost来定义默认的虚拟主机;
属性:name=defaultHost=”localhost”jvmRoute=
·Host组件:位于engine内部用于接收请求并进行相应处理的主机或虚拟主机。
示例:<Host name=”localhost” appBase=”webapps”unpackWARs=”true” autoDeploy=”true”></Host>常用属性说明:(1) appBase:此Host的webapps的默认存放目录,指存放非归档的web应用程序的目录或归档的WAR文件目录路径;(2) autoDeploy:在Tomcat处于运行状态时,将某webapp放置于appBase所定义的目录中时,是否自动将其部署至tomcat;示例:<Host name=”tc1.magedu.com” appBase=”/appdata/webapps” unpackWARs=”true” autoDeploy=”true”></Host>
·Context组件:
示例:<Context path=”/PATH” docBase=”/PATH/TO/SOMEDIR” reloadable=””/>
·Valve组件:
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”prefix=”localhost_access_log” suffix=”.txt”pattern=”%h %l %u %t "%r" %s %b” />Valve存在多种类型:定义访问日志:org.apache.catalina.valves.AccessLogValve定义访问控制:org.apache.catalina.valves.RemoteAddrValve<Valve className=”org.apache.catalina.valves.RemoteAddrValve” deny=”172\.16\.100\.67″/>
manger应用程序:
·vim /etc/tomcat/tomcat-users.xml修改密码即可登录;
基本配置目录:
1.HTTP反代Tomcat:
示例:<VirtualHost *:80>ServerName www.ilinux.ioProxyRequests OffProxyVia OnProxyPreserveHost On<Proxy *>Require all granted</Proxy>ProxyPass / http://www.ilinux.io:8080/ProxyPassReverse / http://www.ilinux.io:8080/<Location />Require all granted</Location></VirtualHost>
2.Nginx反代Tomcat:
示例:location / {proxy_pass http://www.ilinux.io:8080;}
3.Http集群反代:
示例:<proxy balancer://websrvs>BalancerMember http://172.16.42.2:8080BalancerMember http://172.16.42.3:8080ProxySet lbmethod=byrequests</Proxy><VirtualHost *:80>ServerName www.ilinux.ioProxyVia OnProxyRequests OffProxyPreserveHost On<Proxy *>Require all granted</Proxy>ProxyPass / balancer://tcsrvs/ProxyPassReverse / balancer://tcsrvs/<Location />Require all granted</Location></VirtualHost>
4.Nginx集群反代:
示例:upstream websrvs {server 172.16.42.2;server 172.16.42.3;}location / {proxy_pass http://websrvs;}
5.会话粘性:
#Nginx的会话粘性只要在upstream中添加(hash $request_uri consistent)即可,以下讲HTTP的会话粘性;#在顶头添加以下一行;Header add Set-Cookie “ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/” env=BALANCER_ROUTE_CHANGED<proxy balancer://websrvs>BalancerMember http://172.18.100.67:8080 route=TomcatA loadfactor=1BalancerMember http://172.18.100.68:8080 route=TomcatB loadfactor=2ProxySet lbmethod=byrequestsProxySet stickysession=ROUTEID</Proxy><VirtualHost *:80>ServerName lb.magedu.comProxyVia OnProxyRequests OffProxyPreserveHost On<Proxy *>Require all granted</Proxy>ProxyPass / balancer://websrvs/ProxyPassReverse / balancer://websrvs/<Location />Require all granted</Location></VirtualHost>
6.启用管理接口:
#在以上代码中,添加以下代码即可;<Location /status-manager>SetHandler balancer-managerProxyPass !Require all granted</Location>
7.memcache会话保持:
1.在Tomcat主机下载好MSM;lftp 172.18.0.1:/pub/Sources/7.x86_64/msm> mget *2.在Tomcat主机的server.xml中的contxt中添加以下代码;<Context path=”/test” docBase=”/usr/local/tomcat/webapps/test” reloadable=”true”><Manager className=”de.javakaffee.web.msm.MemcachedBackupSessionManager”memcachedNodes=”n1:172.16.42.2:11211,n2:172.16.42.3:11211″failoverNodes=”n1″requestUriIgnorePattern=”.*\.(ico|png|gif|jpg|css|js)$”transcoderFactoryClass=”de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory”/></Context>3.在memcache主机上yum安装memcache;yum -y install memcache
原创文章,作者:z long,如若转载,请注明出处:http://www.178linux.com/84230