标签:tomcat


tomcat单机多实例


需求是: 同一个tomcat服务下,可以访问多个域名。

先来看看tomcat配置文件server.xml的结构

<Server>

<Service name=”xxx”>

<Host  name=”aaa.com” appBase=”dir1″>

</Host>

</Service>

<Server>

说明:

Service下面可以定义监听的端口,包含了<Host></Host>,而<Host></Host>里面定义虚拟主机,即,定义站点的域名和站点的目录。

 

两种方案:

1 可以在<Service></Service>里面定义多个<Host></Host>,这样就定义了多个站点,但端口不能变。

2 可以在<Server></Server>里面定义多个<Service></Service>,<Service></Service>里再定义<Host></Host>,这样做的好处是,可以更改监听端口。

 

假设需求为:

同一个服务器上跑两个站点

www.aaa.com  监听端口是8080

www.bbb.com  监听端口是8081

 

配置文件内容为:

<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”>

<Connector port=”8080″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ />
<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />

<Engine name=”Catalina” defaultHost=”localhost”>

<Realm className=”org.apache.catalina.realm.LockOutRealm”>
<Realm className=”org.apache.catalina.realm.UserDatabaseRealm”
resourceName=”UserDatabase”/>
</Realm>

<Host name=”www.aaa.com” appBase=”webapps”
unpackWARs=”true” autoDeploy=”true”>

<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
prefix=”aaa.com_access” suffix=”.log”
pattern=”%h %l %u %t &quot;%r&quot; %s %b” />

</Host>
</Engine>
</Service>
<Service name=”aming”>

<Connector port=”8081″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ />

<Engine name=”aming” defaultHost=”localhost”>

<Realm className=”org.apache.catalina.realm.LockOutRealm”>
<Realm className=”org.apache.catalina.realm.UserDatabaseRealm”
resourceName=”UserDatabase”/>
</Realm>

<Host name=”www.bbb.com” appBase=”/data/tomcat”
unpackWARs=”true” autoDeploy=”true”>

<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
prefix=”bbb.com_access” suffix=”.log”
pattern=”%h %l %u %t &quot;%r&quot; %s %b” />

</Host>
</Engine>
</Service>

</Server>


nginx 代理tomcat,自定义404页面


需求背景是:

网站基于java,使用tomcat搭建环境,使用nginx作为前端代理。若不做特殊处理,当访问出现404时,将会返回tomcat的404页面,现要求404页面为nginx的自定义页面,并非tomcat自带的404页面。

nginx的配置文件内容如下:

server {

listen 80;

server_name www.xxx.com;

proxy_intercept_errors  on;

recursive_error_pages   on;

location / {

error_page 404 @404;

proxy_pass      http://127.0.0.1:8080;

proxy_set_header Host   $host;

}

location @404 {

root /tmp/111/;

error_page 404 /404.html;

}

}

其中红色字体的两行配置非常关键,404.html的位置在/tmp/111/404.html。

proxy_intercept_errors  on; 当上游服务器响应头回来后,可以根据响应状态码的值进行拦截错误处理,与error_page 指令相互结合。用在访问上游服务器出现错误的情况下。

recursive_error_pages   on; 可以让下面的location @404生效。