为什么我的两个Apache2虚拟主机指向了同一个网站目录?
故障原因分析
- example2.domain.com 配置文件无效
你贴出的example2.domain.com.conf缺失核心的<VirtualHost *:80>开头标签,属于不完整的非法虚拟主机配置,Apache 重载/启动时会直接忽略这段配置,无法识别 80 端口下example2.domain.com对应的站点规则。 - 443 端口无对应虚拟主机配置
目前所有生效的 443 端口虚拟主机只有example.domain.com-le-ssl.conf对应的站点,如果你是通过 HTTPS 访问example2.domain.com,Apache 会将未匹配到专属虚拟主机的 443 请求,默认转发到第一个加载的 443 虚拟主机,也就是example.domain.com的站点,因此会加载/var/www/example目录下的内容,符合你描述的故障表现。 - hosts 域名映射不完整
/etc/hosts中仅配置了短域名example2指向 127.0.0.1,没有配置完整域名example2.domain.com的本地映射,本地测试场景下解析规则也会存在异常。 - 潜在遗漏操作:你可能没有执行
a2ensite example2.domain.com.conf启用该站点配置,也没有重载 Apache 让配置生效。
修复步骤
- 补全
example2.domain.com.conf的配置,参考如下:
<VirtualHost *:80> DocumentRoot /var/www/Dev ServerName example2.domain.com # 重复的ServerAlias可删除,也可保留填写其他别名 ServerAlias www.example2.domain.com <Directory /var/www/Dev/> Options FollowSymlinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/example2_error.log CustomLog ${APACHE_LOG_DIR}/example2_access.log combined # 若需要给example2也配置HTTPS,可添加下方跳转,后续单独申请SSL证书生成443配置即可 # Redirect permanent / https://example2.domain.com/ </VirtualHost>
- 补全
/etc/hosts的域名映射:
127.0.0.1 example2.domain.com example2
- 执行命令启用站点并重载 Apache 生效:
sudo a2ensite example2.domain.com.conf sudo systemctl reload apache2
内容的提问来源于stack exchange,提问作者DauntlessRob
相关产品推荐
相关产品推荐

