当前位置: 代码迷 >> 综合 >> Apache VHost
  详细解决方案

Apache VHost

热度:76   发布时间:2023-12-16 22:22:34.0

简单的添加:
<VirtualHost myhost>
    DocumentRoot usr/local/www/myhost
    ServerName   myhost
</VirtualHost>

在最后加上
NameVirtualHost *
<VirtualHost *>
    rewriteengine on
    rewritecond %{HTTP_HOST} [^.]+/.alibaba/.uni/.cc$
    rewriterule ^(.+) %{HTTP_HOST}$1 [C]
    rewriterule ([^.]+)/.alibaba/.uni/.cc(.*) /$1$2
    ServerAdmin webmaster@XXX.COM
    DocumentRoot /www
    ServerName alibaba.uni.cc
    ErrorLog logs/alibaba.uni.cc-error_log
    LogLevel warn
    #LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent} "" combined
    #LogFormat "%h %l %u %t "%r" %>s %b" common
    #LogFormat "%{Referer}i -> %U" referer
    #LogFormat "%{User-agent}i" agent 
    ##CustomLog logs/log/httpd-access.log common
    ##CustomLog logs/log/httpd-referer.log referer
    ##CustomLog logs/log/httpd-agent.log agent
    #CustomLog logs/httpd-access.log combined
    CustomLog logs/alibaba.uni.cc-access_log common
</VirtualHost>
将http://username.alibaba.uni.cc对于username的主页请求转换为对http://alibaba.uni.cc/username的请求
---- 对于HTTP/1.1的请求包括一个Host: HTTP头,我们能用下面的规则集重写 http://username.alibaba.uni.cc/anypath到/home/username/anypath。
注: “rewritecond”表明是条件重写规则,当满足后面定义的条件后才会应用下面的重写规则,“rewritecond”有各种变量,请查阅相关文档。
其实是用到了APACHE的重写规则,大家可以看
http://alibaba.uni.cc
你可以打开里面的目录像这样打开测试一下
http://skyspy.alibaba.uni.cc

Linux下操作
首先,你的拥有一个有泛域名解析的顶级域名,例如: domain.com
其次,在 httpd.conf 中打开 mod_rewrite
之后,在 httpd.conf 的最后,添加以下内容:
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteMap vhost txt:/usr/local/etc/apache/vhost.map
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1
其中的 /usr/local/etc/apache 是你的 apache 服务器配置文件所在路径,根据实际情况更改。
然后,在这个所在路径的目录下创建一个文件: vhost.map
内容为:
www.domain.com /usr/local/www/data-dist/domain
bbs.domain.com /usr/local/www/data-dist/bbs
anyname.domain.com /usr/local/www/data-dist/anyname
以上部分都是:" 域名+空格+绝对路径" 的形式。
最后,在你的网站根目录 /usr/local/www/data-dist 下,创建对应目录:
domain , bbs , anyname 等等,理论上可以无限。
这样,通过浏览器,访问 www.domain.com 时,实际上访问的就是 /usr/local/www/data-dist/domain目录下的文件。同理,访问 bbs.domain.com 实际上访问的就是 /usr/local/www/data-dist/bbs 目录下的文件。而且,你可以
随时更改 vhost.map 来增加、删除、修改你的二级域名和所指向的实际路径,不用重启 apache。

接下来windows下的操作:
1、域名物理地址文件
C:/AppServ/Apache2.2/conf/vhost.map
book.tiyan.com C:/AppServ/www/book
xxx.tiyan.com   C:/AppServ/www/xxx
2、开启LoadModule rewrite_module modules/mod_rewrite.so
3、域名泛解析
4、以下配置添加到httpd.conf的末尾或VirtualHost中
RewriteLog         logs/rewrite.log
RewriteLogLevel    0
RewriteEngine      on
RewriteMap         lowercase int:tolower
RewriteMap         vhost txt:C:/AppServ/Apache2.2/conf/vhost.map
RewriteCond        ${lowercase:%{HTTP_HOST}|NONE} ^(.+)$
RewriteCond        ${vhost:%1} ^(C:/.*)$
RewriteRule        ^/(.*)$ %1/$1 [E=VHOST:${lowercase:%{HTTP_HOST}}]
要点:
a.正则^(.+)$ 要和vhost.map的第一部分匹配,在这个例子中^(.+)$是可以匹配book.tiyan.com的
b.正则^(C:/.*)$要和vhost.map的第二部分匹配,在这个例子中^(C:/.*)$是可以匹配C:/AppServ/www/book的
c.RewriteCond        ${vhost:%1} ^(C:/.*)$
把盘符C:加入到正则中,如果您的web文件不是放在C盘而是E盘,要把C:改为E:
随时更改 vhost.map 来增加、删除、修改你的域名和所指向的实际路径,不用重启 Apache

 

 

 

 

 

 

VirtualHost  overlaps with VirtualHost , the first has precedence, perhaps you need a NameVirtualHost directive

Solution:
Add the following line in your /etc/apache2/http.conf
NameVirtualHost *:80
Create a virtualhost directive for your virtual domain. You can add it /etc/apache2/http.conf file or you can create a separate file in /etc/apache2/sites-available directory
<VirtualHost *:80>
ServerName www.domain.com
ServerAdmin admin@domain.com
ServerAlias domain.com
DocumentRoot /home/domain.com
ErrorLog /var/log/apache2/domain.com-error.log
CustomLog /var/log/apache2/domain.com-access.log
</VirtualHost>
If you want to use /etc/apache2/sites-enabled, make sure that the following line is in /etc/apache2/apache.conf
Include /etc/apache2/sites-enabled/[^.#]*

Your VirtualHost directives are slightly incorrect.
First of all, before you assign several domains to the same IP, you need to add a "NameVirtualHost" parameter for that IP Address. Here's an example:
NameVirtualHost *:80
Next, you need to update your VirtualHost tags to MATCH the "NameVirtualHost" parameter, like so:
<VirtualHost *:80>
  DocumentRoot "/var/www/webdav/block"
  ServerName block.dev.pd
  ServerAlias block.dev.pd
  <Directory "/var/www/webdav/block">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

  相关解决方案