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

Apache2通配符子域名虚拟主机DocumentRoot配置问题求助

Apache子域名自动映射DocumentRoot配置修复

你的配置失效的核心原因是VirtualDocumentRoot的变量拼接错误,导致生成的路径与实际目标目录不匹配。以下是两种可行的修复方案:

方案一:通配符快速匹配

这种方式简单直接,利用Apache的通配符匹配捕获完整子域名前缀:

<VirtualHost *:80>       
    ServerName wp.example.com        
    ServerAlias *-web.example.com
    ServerAdmin webmaster@localhost
    # %1会直接捕获"demo-web"这类完整前缀,映射到对应目录
    VirtualDocumentRoot /var/www/html/%1
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

方案二:正则表达式精准匹配

如果需要更严谨的匹配规则,用正则捕获子域名前缀部分:

<VirtualHost *:80>       
    ServerName wp.example.com        
    # 仅匹配以"-web.example.com"结尾的子域名
    ServerAlias ^(.*)-web\.example\.com$
    ServerAdmin webmaster@localhost
    # %1捕获正则分组中的"demo"部分,拼接成目标路径
    VirtualDocumentRoot /var/www/html/%1-web
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

必要操作

  1. 确保mod_vhost_alias模块已启用:
    # Debian/Ubuntu系统
    a2enmod vhost_alias
    # RHEL/CentOS系统,先检查是否启用
    httpd -t -D DUMP_MODULES | grep vhost_alias
    # 未启用则编辑httpd.conf添加:LoadModule vhost_alias_module modules/mod_vhost_alias.so
    
  2. 验证配置语法并重启Apache:
    apache2ctl configtest && systemctl restart apache2  # Debian/Ubuntu
    # 或
    httpd -t && systemctl restart httpd  # RHEL/CentOS
    
  3. 确认目标目录(如/var/www/html/demo-web)存在且Apache拥有读写权限。

内容的提问来源于stack exchange,提问作者Ankur Chandravanshi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 02:05:38