您的位置:首页 > 运维架构 > Tomcat

Tomcat虚拟主机

2015-05-30 17:59 691 查看
通过配置tomcat虚拟主机,可以实现一个tomcat服务器承载多个Web站点

实现原理:多个虚拟主机的Web站点是创建在同一台计算机上的,不管Web浏览器访问其中的哪一个站点,请求都会发送到这台计算机上。怎么区分的?

1.Web服务器上的每个Web站点必须设置不同的标识信息
//我要有识别你的办法

2.Web浏览器发出的连接和请求信息必须包含Web站点的表示信息
//那前提是你们有不同标识

实现方法:

1.基于主机名的虚拟主机:

通过请求信息中的Host: 字段来判断进入那个主机

%CATALINA_HOME%/conf/server.xml
中的各种标签:

<Engine name="Standalone" default="localhost" debug="0">
//处理客户端请求的引擎,这里如果没有检测的主机名,则去进入localhost的主机

<Host name="localhost" appBase="webapps">
//对应的是一个主机,刚好可以与Http请求中的Host信息相对应

<Context path="" docBase="" debug="0">
//对应的是一个映射路径

。。。

</Context>

...

</Host>

</Engine>

我在d:下创建了2个文件夹:VistualHost1/test.html:这是vistualHost1中的test.html
VistualHost2/test.html:这是vistuallHost2中的test2.html

在server.xml文件中的修改,在Host之下添上如下:

<span style="white-space:pre">	</span><Host>... </Host>
<span style="white-space:pre">	</span><Host name="site1" debug="0" appBase="d:/VirtualHost1" 
       <span style="white-space:pre">	</span>unpackWARs="true" autoDeploy="true">
       <span style="white-space:pre">		</span><Context path="" docBase="." />
       <span style="white-space:pre">	</span></Host>
        <Host name="site2" debug="0" appBase="d:/VirtualHost2" 
      <span style="white-space:pre">	</span> unpackWARs="true" autoDeploy="true">
       <span style="white-space:pre">		</span><Context path="" docBase="." />
       <span style="white-space:pre">	</span></Host>
    </Engine>


然后通过java代码访问测试

package com.aii.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Main {

	public static void main(String[] args)throws Exception {
		send("");
		send("site1");
		send("site2");
	}
	
	public static void send(String args)throws Exception{
		InetAddress address=InetAddress.getLocalHost();
		Socket socket=new Socket(address, 8080);
		BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));		
		PrintWriter pw=new PrintWriter(socket.getOutputStream());
		pw.println("GET /test.html HTTP/1.1");
		pw.println("Host: "+args);
		pw.println();
		pw.flush();
		for(String str=reader.readLine();str!=null;){
			System.out.println(str);
			str=reader.readLine();
		}
		reader.close();
		socket.close();
	}
}
打印

效果如下:

HTTP/1.1 200 OK
ETag: W/"9-1432968564586"
Last-Modified: Sat, 30 May 2015 06:49:24 GMT
Content-Type: text/html
Content-Length: 9
Date: Sat, 30 May 2015 09:46:06 GMT
Server: Apache Coyote/1.0

test Page
HTTP/1.1 200 OK
ETag: W/"31-1432977236036"
Last-Modified: Sat, 30 May 2015 09:13:56 GMT
Content-Type: text/html
Content-Length: 31
Date: Sat, 30 May 2015 09:46:26 GMT
Server: Apache Coyote/1.0

这是vistualHost1中的test.html
HTTP/1.1 200 OK
ETag: W/"31-1432977250959"
Last-Modified: Sat, 30 May 2015 09:14:10 GMT
Content-Type: text/html
Content-Length: 31
Date: Sat, 30 May 2015 09:46:46 GMT
Server: Apache Coyote/1.0

这是vistualHost2中的test.html


评价:共享同一个IP地址和端口号,Web浏览不能通过IP地址去访问这些Web站点,用的还是比较多的

2.基于端口号的虚拟主机

不同的端口对应不同的Web服务器

<Service name="">

<Connector port=""../>

<Engine ...>

<Context..></Context>

</Engine>

将一个或者多个<Connector>元素与某个<Engine>元素相关联的方式就是将他们嵌套在一个<Service>中

由于需要指定不同的端口号,而我们一般访问的网页用的都是80端口,也就是不写,所以用的不多

3.基于IP地址的虚拟主机
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: