Apache虚拟主机的配置

作者:wind521 来源:bbs.chinaunix.net 时间:2007-08-20 15:58:00 

一、使用 mod_vhost_alias

1、简单的动态虚拟主机

# 从 Host: 头中取得服务器名字 Server Name

UseCanonicalName Off

# 这里的日志格式,可以在将来通过第一个参数域来分隔不同的虚拟主机的日志

LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon

CustomLog logs/access_log vcommon

# 在返回请求的文件名的路径中包含进服务器名字: server name

VirtualDocumentRoot /www/hosts/%0/docs

VirtualScriptAlias /www/hosts/%0/cgi-bin

2、更为有效的基于 IP 地址的虚拟主机

# 从 IP 地址反解析得到服务器名字(server name)

UseCanonicalName DNS

# 在日志中包含 IP 地址,便于后续分发

LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon

CustomLog logs/access_log vcommon

# 在文件路径中包含 IP 地址

VirtualDocumentRootIP /www/hosts/%0/docs

VirtualScriptAliasIP /www/hosts/%0/cgi-bin

二、使用 mod_rewrite

1、使用 mod_rewrite 实现简单的动态虚拟主机

# 从 Host: 头获取服务器名字

UseCanonicalName Off

# 可分割的日志

LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon

CustomLog logs/access_log vcommon

<Directory /www/hosts>

# ExecCGI is needed here because we can't force

# CGI execution in the way that ScriptAlias does

Options FollowSymLinks ExecCGI

</Directory>

# 接下来是关键部分

RewriteEngine On

# a ServerName derived from a Host: header may be any case at all RewriteMap lowercase int:tolower

## 首先处理普通文档:

# 允许变名 /icons/ 起作用 - 其他变名类同

RewriteCond %{REQUEST_URI} !^/icons/

# 允许 CGIs

RewriteCond %{REQUEST_URI} !^/cgi-bin/

# 开始&ldquo;变戏法&rdquo;

RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1

## 现在处理 CGIs - 我们需要强制使用一个 MIME 类型

RewriteCond %{REQUEST_URI} ^/cgi-bin/

RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [T=application/x-httpd-cgi]

# 好了!

2、使用独立的虚拟主机配置文件 vhost.map

vhost.map 文件包含了类似下面的内容:

www.customer-1.com /www/customers/1

www.customer-2.com /www/customers/2

# ...

www.customer-N.com /www/customers/N

http.conf 包含了:

RewriteEngine on

RewriteMap lowercase int:tolower

# 定义映像文件

RewriteMap vhost txt:/www/conf/vhost.map

# 和上面的例子一样,处理变名

RewriteCond %{REQUEST_URI} !^/icons/

RewriteCond %{REQUEST_URI} !^/cgi-bin/

RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$

# 这里做基于文件的重新映射

RewriteCond ${vhost:%1} ^(/.*)$

RewriteRule ^/(.*)$ %1/docs/$1

RewriteCond %{REQUEST_URI} ^/cgi-bin/

RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$

RewriteCond ${vhost:%1} ^(/.*)$

RewriteRule ^/(.*)$ %1/cgi-bin/$1

标签:
0
投稿

猜你喜欢

  • 一份详细的服务器安全解决方案

    2008-08-16 17:11:00
  • 百度更新时间一览

    2007-12-07 18:53:00
  • 谈谈百度收录那点破事

    2009-02-02 10:21:00
  • DEDE:footer调用新方法,解决整站调用footer

    2009-05-12 10:49:00
  • IIS错误代码大汇总

    2010-04-05 18:36:00
  • 使用nginx实现网站负载均衡测试实例

    2009-11-19 11:46:00
  • 怎样学好搜索引擎优化SEO?

    2009-02-24 08:51:00
  • 十个月聚集15万站长会员 站长论坛是如何做到的

    2009-02-10 18:08:00
  • 分析Google搜索结果页变化 窥见优化方向

    2009-09-15 09:12:00
  • 网站内部链接建设合理化很重要

    2009-02-20 09:33:00
  • 世界博客服务稳定性评测

    2009-03-30 16:18:00
  • 草根站长:网站推广的五点实战经验

    2010-03-03 11:08:00
  • 微软Office 2010 Beta版提前泄露于P2P网站

    2009-11-14 09:04:00
  • GoDaddy常见问题之与主机有关的用户帐户解释

    2010-04-13 13:03:00
  • 竞合将是未来视频行业的主旋律

    2009-10-16 09:34:00
  • Web服务器安装和运行FTP操作步骤

    2009-11-13 15:01:00
  • 网游业高薪? 中国游戏行业五大愚人新闻

    2008-09-25 17:40:00
  • Dedecms与Ecms的战争已入白热化

    2007-11-22 23:56:00
  • 网站推“帖子换房子” 网友纷纷质疑网站炒作

    2009-10-12 09:31:00
  • 十二个月内 仅仅依靠Google打造成功网站

    2009-02-04 16:59:00
  • asp之家 网站运营 m.aspxhome.com