---
title: "Apache VirtualHost的用法 "
author: "carrot"
date: "2008-08-25T21:07:38Z"
tags: []
slug: "apache-virtualhost"
draft: false
lastmod: "2008-08-25T21:07:38Z"
id: 232558
url: "https://carrot.iteye.com/blog/232558"
---

这么一个需求：同一台服务器上有两个应用，如  
[http://hostA:8080/services](http://hostA:8080/services)和[http://hostA:8080/admin](http://hostA:8080/admin)  
外部访问时，需要从不同的域名访问,如  
[http://services.host.com](http://services.host.com)和[http://admin.host.com](http://admin.host.com)  
一开始给他们这么一个比较简单的解决方案：  
分别把services和admin两个应用，部署到不同的两个端口上，如  
services -&gt; [http://hostA:8081/](http://hostA:8081/)  
admin -&gt; [http://hostA:8082/](http://hostA:8082/)  
接着在防火墙配两个公网IP，然后dns上把services.host.com和admin.host.com配置到这两个IP上。  
当请求到达防火墙时，防火墙根据所访问的ip转发到hostA的对应端口上。  
前 方用的防火墙是我们公司的Audemon 100，和公司的Audemon系统组的交流后得知，目前的防火墙版本不支持同时配置两个ip，要到六月底才能出版本支持。！@…%#%…%￥，晕倒，好 像这是很基本的功能来的吧，居然还不支持。没办法，此路不通。由于防火墙是不管域名的(因为域名资料是通过应用层传输的)，那更别指望防火墙根据域名转发 了。  
因此，我们只好提供了软件级的解决方案，让前方在Tomcat前加一个Apache 2.2，通过Apache的Virtual Host + AJP实现转发。  
Apache的部分配置如下：  
NameVirtualHost \*:80  
&lt;VirtualHost \*:80&gt;  
ServerName host1.com  
ProxyPass / ajp://host1.com:8009/  
ProxyPassReverse / ajp://host1.com:8009/  
&lt;/VirtualHost&gt;  
&lt;VirtualHost \*:80&gt;  
ServerName host2.com  
ProxyPass / ajp://host2.com:8019/  
ProxyPassReverse / ajp://host2.com:8019/  
&lt;/VirtualHost&gt;  
Tomcat也需要配置AJP Connector，如host1.com的配置  
&lt;Connector port=”8009″ enableLookups=”false” redirectPort=”8443″ protocol=”AJP/1.3″ /&gt;  
这个方案相对于防火墙的硬件方案，性能上要差一些，但还是不错的。  
另 外还有一种方案是通过iptables的domain module来实现，但这个domain module好像是国内的某个高手写的，只更新到v0.02版本，并没有提交到iptables的标准里。虽然可以用而且性能比Apache的方案要高一 些，但是风险较大，而且配置麻烦（既要编译内核，又要配置iptables的rules），所以没有用这种方式。  
本文来源于 冰山上的播客 [http://xinsync.xju.edu.cn](http://xinsync.xju.edu.cn) , 原文地址：[http://xinsync.xju.edu.cn/index.php/archives/115](http://xinsync.xju.edu.cn/index.php/archives/115)

 

 

 

&lt;Directory "/var/www/www"&gt;  
    Options FollowSymLinks  
    AllowOverride None  
    Order allow,deny  
    Allow from all  
&lt;/Directory&gt;  
&lt;Directory "/var/www/phorum"&gt;  
    Options FollowSymLinks  
    AllowOverride None  
    Order allow,deny  
    Allow from all  
&lt;/Directory&gt;

\# 针对三部主机的 DocumentRoot 进行定！  
&lt;VirtualHost \*:80&gt;  
    ServerName    linux.vbird.tsai  
    DocumentRoot  /var/www/html  
&lt;/VirtualHost&gt;  
&lt;VirtualHost \*:80&gt;  
    ServerName    www.vbird.tsai  
    DocumentRoot  /var/www/www  
    CustomLog     /var/log/httpd/www.access\_log combined  
    # 不同的主页可以指定不同的登录档资讯，这样比较好 debug 与分析啦！  
&lt;/VirtualHost&gt;  
&lt;VirtualHost \*:80&gt;  
    ServerName    phorum.vbird.tsai  
    DocumentRoot  /var/www/phorum  
&lt;/VirtualHost&gt;

 

 

> `<VirtualHost *> ServerAdmin xxx@xxx.org ServerName xxxxx ProxyRequests Off <Proxy *> Order allow,deny Allow from all </Proxy> # reverse proxy setting ProxyPass / http://192.168.100.8:8180/ ProxyPassReverse / http://192.168.100.8:8180/ # cache dir root CacheRoot "/var/www/proxy" # max cache storage CacheSize 50000000 # hour: every 4 hour CacheGcInterval 4 # max page expire time: hour CacheMaxExpire 240 # Expire time = (now - last_modified) * CacheLastModifiedFactor CacheLastModifiedFactor 0.1 # defalt expire tag: hour CacheDefaultExpire 1 # force complete after precent of content retrived: 60-90% CacheForceCompletion 80`
> 
> ErrorLog /var/log/apache2/error-westdc.geodata.cn.log  
> CustomLog /var/log/apache2/access-westdc.geodata.cn.log combined  
> ServerSignature On
> 
> &lt;/VirtualHost&gt;