tomcat应用实践(虚拟主机以及站点优化)
目前主流的Web开发编程语言有php,java以及.net等,但是大多数架构都会选择java作为开发语言,所以很多java应用容器很受大家欢迎,例如tomcat、jetty、resin,jboss等。我们是使用tomcat。接下来就介绍一下tomcat虚拟主机以及实际使用中的问题。
tomcat 虚拟主机
使用过tomcat的童鞋都知道tomcat是默认的8080端口,而web默认的端口是80端口,同时还需要注意Linux系统里,非root权限用户不能使用1024以下的端口,对于一些服务,过高的权限,会带来一定的风险。但是一般在站点后面加端口和项目路径等是一种体验不是很好的方式。瞬间让使用者各种不爽,这时候我们就需要用一些技术手段去实现这些。
常见的方法有:
参考文档:http://blog.csdn.net/becivells/article/details/52842019 (本篇文档不会研究)
1.nginx 等软件做反向代理
2.iptables端口转发
首先程序绑定1024以上的端口,然后root权限下做转发注意有些系统需要手动开启IP FORWARD功能
vi/etc/sysctl.conf#修改net.ipv4.ip_forward=1#重新加载sysctl-p/etc/sysctl.conf
iptables-APREROUTING-tnat-ptcp--dport80-jREDIRECT--to-port8080
这里简单的介绍一下一些虚拟主机的知识。
在iis和apache等里面我们可以创建基于不同端口或域名的虚拟主机,本次主要介绍tomcat的虚拟主机等知识。
如何在tomcat里运行一个java的web项目呢?
例如项目名为52lqian,我们将整理打包好的52lqian.war包上传到tomcat下的webapps目录下,重启tomcat容器。我们就可以用以下方式访问该站点。
http://IP:8080/52lqian 方式访问 ,而http://IP:8080/这种方式默认是访问的webapps目录下的ROOT目录的内容,主要就是tomcat的一些介绍信息。
这样访问比较繁琐的,可以使用虚拟主机和虚拟目录来实现只需http://IP:8080/访问。
<?xmlversion='1.0'encoding='utf-8'?><!--LicensedtotheApacheSoftwareFoundation(ASF)underoneormorecontributorlicenseagreements.SeetheNOTICEfiledistributedwiththisworkforadditionalinformationregardingcopyrightownership.TheASFlicensesthisfiletoYouundertheApacheLicense,Version2.0(the"License");youmaynotusethisfileexceptincompliancewiththeLicense.YoumayobtainacopyoftheLicenseathttp://www.apache.org/licenses/LICENSE-2.0Unlessrequiredbyapplicablelaworagreedtoinwriting,softwaredistributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.SeetheLicenseforthespecificlanguagegoverningpermissionsandlimitationsundertheLicense.--><!--Note:A"Server"isnotitselfa"Container",soyoumaynotdefinesubcomponentssuchas"Valves"atthislevel.Documentationat/docs/config/server.html--><Serverport="-1"shutdown="SHUTDOWN"><ListenerclassName="org.apache.catalina.startup.VersionLoggerListener"/><!--Securitylistener.Documentationat/docs/config/listeners.html<ListenerclassName="org.apache.catalina.security.SecurityListener"/>--><!--APRlibraryloader.Documentationat/docs/apr.html--><ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on"/><!--InitializeJasperpriortowebappsareloaded.Documentationat/docs/jasper-howto.html--><ListenerclassName="org.apache.catalina.core.JasperListener"/><!--Preventmemoryleaksduetouseofparticularjava/javaxAPIs--><ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/><ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/><ListenerclassName="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/><!--GlobalJNDIresourcesDocumentationat/docs/jndi-resources-howto.html--><GlobalNamingResources><!--EditableuserdatabasethatcanalsobeusedbyUserDatabaseRealmtoauthenticateusers--><Resourcename="UserDatabase"auth="Container"type="org.apache.catalina.UserDatabase"description="Userdatabasethatcanbeupdatedandsaved"factory="org.apache.catalina.users.MemoryUserDatabaseFactory"pathname="conf/tomcat-users.xml"/></GlobalNamingResources><!--A"Service"isacollectionofoneormore"Connectors"thatshareasingle"Container"Note:A"Service"isnotitselfa"Container",soyoumaynotdefinesubcomponentssuchas"Valves"atthislevel.Documentationat/docs/config/service.html--><Servicename="Catalina"><!--Theconnectorscanuseasharedexecutor,youcandefineoneormorenamedthreadpools--><!--<Executorname="tomcatThreadPool"namePrefix="catalina-exec-"maxThreads="150"minSpareThreads="4"/>--><!--A"Connector"representsanendpointbywhichrequestsarereceivedandresponsesarereturned.Documentationat:JavaHTTPConnector:/docs/config/http.html(blocking&non-blocking)JavaAJPConnector:/docs/config/ajp.htmlAPR(HTTP/AJP)Connector:/docs/apr.htmlDefineanon-SSLHTTP/1.1Connectoronport8080--><Connectorport="80"protocol="HTTP/1.1"#修改为80端口不需要附加端口connectionTimeout="20000"redirectPort="8443"maxThreads="150"minSpareThreads="25"maxSpareThreads="75"enableLookups="false"acceptCount="100"debug="0"disableUploadTimeout="true"/><!--A"Connector"usingthesharedthreadpool--><!--<Connectorexecutor="tomcatThreadPool"port="8080"protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443"/>--><!--DefineaSSLHTTP/1.1Connectoronport8443ThisconnectorusestheBIOimplementationthatrequirestheJSSEstyleconfiguration.WhenusingtheAPR/nativeimplementation,theOpenSSLstyleconfigurationisrequiredasdescribedintheAPR/nativedocumentation--><!--<Connectorport="8443"protocol="org.apache.coyote.http11.Http11Protocol"maxThreads="150"SSLEnabled="true"scheme="https"secure="true"clientAuth="false"sslProtocol="TLS"/>--><!--DefineanAJP1.3Connectoronport8009--><Connectorport="8052"protocol="AJP/1.3"redirectPort="8443"/><!--AnEnginerepresentstheentrypoint(withinCatalina)thatprocesseseveryrequest.TheEngineimplementationforTomcatstandaloneanalyzestheHTTPheadersincludedwiththerequest,andpassesthemontotheappropriateHost(virtualhost).Documentationat/docs/config/engine.html--><!--YoushouldsetjvmRoutetosupportload-balancingviaAJPie:<Enginename="Catalina"defaultHost="localhost"jvmRoute="jvm1">--><Enginename="Catalina"defaultHost="www.52laiqian.com"><!--Forclustering,pleasetakealookatdocumentationat:/docs/cluster-howto.html(simplehowto)/docs/config/cluster.html(referencedocumentation)--><!--<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>--><!--UsetheLockOutRealmtopreventattemptstoguessuserpasswordsviaabrute-forceattack--><RealmclassName="org.apache.catalina.realm.LockOutRealm"><!--ThisRealmusestheUserDatabaseconfiguredintheglobalJNDIresourcesunderthekey"UserDatabase".AnyeditsthatareperformedagainstthisUserDatabaseareimmediatelyavailableforusebytheRealm.--><RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/></Realm><Hostname="localhost"appBase="webapps"unpackWARs="true"autoDeploy="true"><!--SingleSignOnvalve,shareauthenticationbetweenwebapplicationsDocumentationat:/docs/config/valve.html--><!--<ValveclassName="org.apache.catalina.authenticator.SingleSignOn"/>--><!--Accesslogprocessesallexample.Documentationat:/docs/config/valve.htmlNote:Thepatternusedisequivalenttousingpattern="common"--><ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"prefix="localhost_access_log."suffix=".txt"pattern="%h%l%u%t"%r"%s%b"/></Host>#添加基于域名的host站点<Hostname="www.52laiqian.com"appBase="webapps"unpackWARs="true"autoDeploy="true"><Contextpath=""docBase="52lqian/"/>#创建虚拟目录,这样就不需要输入项目名可以直接访问<!--SingleSignOnvalve,shareauthenticationbetweenwebapplicationsDocumentationat:/docs/config/valve.html--><!--<ValveclassName="org.apache.catalina.authenticator.SingleSignOn"/>--><!--Accesslogprocessesallexample.Documentationat:/docs/config/valve.htmlNote:Thepatternusedisequivalenttousingpattern="common"--><ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"prefix="localhost_access_log."suffix=".txt"pattern="%h%l%u%t"%r"%s%b"/></Host></Engine></Service></Server>
主要更改配置
<Hostname="www.52laiqian.com"appBase="webapps"unpackWARs="true"autoDeploy="true"><Contextpath=""docBase="52lqian/"/>#创建虚拟目录,这样就不需要输入项目名可以直接访问<!--SingleSignOnvalve,shareauthenticationbetweenwebapplicationsDocumentationat:/docs/config/valve.html--><!--<ValveclassName="org.apache.catalina.authenticator.SingleSignOn"/>--><!--Accesslogprocessesallexample.Documentationat:/docs/config/valve.htmlNote:Thepatternusedisequivalenttousingpattern="common"--><ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"prefix="localhost_access_log."suffix=".txt"pattern="%h%l%u%t"%r"%s%b"/></Host>
这时就可以直接通过域名访问,(前提是需要域名解析已经设置好)
站点访问慢
整个站点是新开发的站点,部署上去首次打开要几分钟,打开页面直接是够够的,这种体验连自己都受不了。所以就考虑优化,首先对java_OPS的优化。
$tomcat/bin/catalina.sh 文件
JAVA_OPTS="-Djava.awt.headless=true-Dfile.encoding=UTF-8-server-Xms1024m-Xmx1024m-XX:NewSize=256m-XX:MaxNewSize=256m-XX:PermSize=512m-XX:MaxPermSize=256m-XX:+DisableExplicitGC"
tomcat里面的优化解决DNS解析,有时候真的很烦DNS解析,SSH连接 mysql连接等都会受DNS解析延时。而tomcat也会受此影响的。
<Connectorport="80"protocol="HTTP/1.1"#修改为80端口不需要附加端口connectionTimeout="20000"redirectPort="8443"maxThreads="150"minSpareThreads="25"maxSpareThreads="75"enableLookups="false"#关闭DNS解析acceptCount="100"debug="0"disableUploadTimeout="true"/>
重新访问会快很多,以此解决此类问题。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。