主主机和虚拟主机的 Apache 配置

主主机和虚拟主机的 Apache 配置

我已经配置了主主机 (doc root /var/www/html) 和虚拟主机 (doc root /var/www/html/vhosts ),但是当我访问主主机 (server2.example.com) 时,它总是被转发到虚拟主机 (主主机消失)。下面,我提供了 httpd.conf,请帮助我。

ServerAdmin root@localhost
ServerName server2.example.com:80
UseCanonicalName Off
DocumentRoot "/var/www/html"
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot /var/www/html/web/test1.example.com
    ServerName test1.example.com
    ServerAdmin [email protected]
    ErrorLog logs/test1.example.com-error_log
    CUstomLog logs/test1.example.com-access_log common
<Directory "/var/www/html/web/test1.example.com">
    order deny,allow
    deny from all
    allow from 192.168.122.1 
</Directory>
</VirtualHost>

提前致谢。

答案1

Apache 提供第一个与 Host: 标头匹配的虚拟主机或第一个定义的虚拟主机(如果没有虚拟主机与 Host: 标头匹配),因此,当使用基于名称的虚拟主机时,请将两个主机都设为虚拟主机,并将您想要作为默认主机的主机放在首位。

对于除 test1.example.com 之外的任何内容,这应该为 server2.example.com 提供服务。

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin root@localhost
    ServerName server2.example.com:80
    UseCanonicalName Off
    DocumentRoot "/var/www/html"
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory "/var/www/html">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</virtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/web/test1.example.com
    ServerName test1.example.com
    ServerAdmin [email protected]
    ErrorLog logs/test1.example.com-error_log
    CUstomLog logs/test1.example.com-access_log common
    <Directory "/var/www/html/web/test1.example.com">
        order deny,allow
        deny from all
        allow from 192.168.122.1 
    </Directory>
</VirtualHost>

相关内容