Apache虚拟主机显示默认页求助:访问域名显默认页,加目录正常
先把你的配置片段整理好方便查看:
NameVirtualHost ***.**.**.**:81 NameVirtualHost ***.**.**.**:82 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <VirtualHost> block. # #LoadModule proxy_module modules/mod_proxy.so #ProxyRequests On <VirtualHost ***.**.**.**:81> ServerName mrh.star.com.tn <Directory "/var/www/html/mrh"> Options +ExecCGI Indexes MultiViews FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> ServerAdmin webmaster@star.com.tn DocumentRoot "/var/www/html/mrh" ErrorLog "logs/mrh-error.log" CustomLog "logs/mrh.log" common </VirtualHost> <VirtualHost ***.**.**.**:82> ServerName voyage.star.com.tn <Directory "/var/www/html/voyage"> Options +ExecCGI Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ServerAdmin webmaster@star.com.tn DocumentRoot "/var/www/html/voyage" ErrorLog "logs/voyage-error.log" CustomLog "logs/voyage.log" common </VirtualHost>
结合你说的「访问域名显示默认页,但加目录能正常访问」的情况,给你几个具体的排查方向:
先确认虚拟主机模块是否加载:运行
apachectl -M(或httpd -M,根据你的系统调整),看看输出里有没有vhost_alias_module (shared)。如果没有,需要在主配置里添加LoadModule vhost_alias_module modules/mod_vhost_alias.so(路径可能需要根据你的系统调整),然后重启Apache。检查默认站点配置是否冲突:Apache会优先匹配第一个未匹配到ServerName的VirtualHost,或者系统默认的站点配置(比如
000-default.conf)。你可以看看默认配置里有没有监听81/82端口的<VirtualHost>块,如果有,它会优先于你指定IP的配置生效。解决办法要么把你的VirtualHost配置放在默认配置前面,要么直接禁用默认站点(Debian/Ubuntu系统用a2dissite 000-default.conf)。验证端口监听配置:检查Apache的主配置文件(比如
httpd.conf或ports.conf)里有没有Listen ***.**.**.**:81和Listen ***.**.**.**:82这两行。如果只监听了80端口,那访问81/82时请求可能没被正确路由到你的VirtualHost。确认域名解析/本地hosts配置:如果是本地测试,确保你的hosts文件里已经把
mrh.star.com.tn和voyage.star.com.tn指向服务器IP;如果是公网访问,检查DNS解析是否正确。如果Host头和ServerName不匹配,Apache会直接跳转到默认页。检查DocumentRoot的索引文件和权限:虽然加目录能访问,但还是确认下
/var/www/html/mrh和/var/www/html/voyage下有没有默认索引文件(比如index.html、index.php)。你的Directory配置里开了Indexes,没索引文件应该显示目录列表,但如果Apache找不到合适的索引,也可能 fallback到默认页。另外,确保Apache运行用户(比如www-data、apache)对这些目录有读权限,用ls -l /var/www/html/就能查看。验证配置语法并重启服务:每次改完配置一定要运行
apachectl configtest检查语法,没问题再用systemctl restart apache2(或service httpd restart)重启。有时候配置有语法错误,Apache会自动回滚到旧配置,导致你的VirtualHost没生效。查看访问日志排查请求路由:虽然错误日志没内容,但看看
mrh.log和voyage.log里有没有你的请求记录。如果没有,说明请求根本没到对应的VirtualHost(大概率是端口/解析问题);如果有,检查日志里的Host头是不是和你的ServerName一致,不一致的话Apache匹配不到。
另外,你可以做个快速测试:在/var/www/html/mrh里放一个简单的index.html(比如写「MRH测试页」),然后直接访问http://你的服务器IP:81。如果显示这个测试页,说明是域名解析的问题;如果还是默认页,那就是VirtualHost配置没被Apache正确加载。
内容的提问来源于stack exchange,提问作者Khaled Tlili




