You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么我的两个Apache2虚拟主机指向了同一个网站目录?

故障原因分析
  1. example2.domain.com 配置文件无效
    你贴出的 example2.domain.com.conf 缺失核心的 <VirtualHost *:80> 开头标签,属于不完整的非法虚拟主机配置,Apache 重载/启动时会直接忽略这段配置,无法识别 80 端口下 example2.domain.com 对应的站点规则。
  2. 443 端口无对应虚拟主机配置
    目前所有生效的 443 端口虚拟主机只有 example.domain.com-le-ssl.conf 对应的站点,如果你是通过 HTTPS 访问 example2.domain.com,Apache 会将未匹配到专属虚拟主机的 443 请求,默认转发到第一个加载的 443 虚拟主机,也就是 example.domain.com 的站点,因此会加载 /var/www/example 目录下的内容,符合你描述的故障表现。
  3. hosts 域名映射不完整
    /etc/hosts 中仅配置了短域名 example2 指向 127.0.0.1,没有配置完整域名 example2.domain.com 的本地映射,本地测试场景下解析规则也会存在异常。
  4. 潜在遗漏操作:你可能没有执行 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 10:45:03