tomcat应用实践(虚拟主机以及站点优化)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-port 8080
这里简单的介绍一下一些虚拟主机的知识。
在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/访问。
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="-1" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector port="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" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the BIO implementation that requires the JSSE style configuration. When using the APR/native implementation, the OpenSSL style configuration is required as described in the APR/native documentation --> <!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8052" protocol="AJP/1.3" redirectPort="8443" /> <!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html --> <!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost="www.52laiqian.com"> <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference documentation) --> <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> <!-- Use the LockOutRealm to prevent attempts to guess user passwords via a brute-force attack --> <Realm className="org.apache.catalina.realm.LockOutRealm"> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> #添加基于域名的host站点 <Host name="www.52laiqian.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase ="52lqian/"/> #创建虚拟目录,这样就不需要输入项目名可以直接访问 <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="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>
主要更改配置
<Host name="www.52laiqian.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase ="52lqian/"/> #创建虚拟目录,这样就不需要输入项目名可以直接访问 <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="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也会受此影响的。
<Connector port="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" />
重新访问会快很多,以此解决此类问题。
以上是关于tomcat应用实践(虚拟主机以及站点优化)的主要内容,如果未能解决你的问题,请参考以下文章