如何调试 Apache2 虚拟主机

如何调试 Apache2 虚拟主机

我在一台服务器上托管两个网站。每个网站都定义了自己的 VirtualHost。第一个网站运行良好,但当我尝试访问第二个网站时,我被重定向到第一个网站。这就像 Apache 无法通过 ServerName 识别正确的 VirtualHost,并将我重定向到默认(第一个)网站。

是否有日志或其他东西可以让我查看 Apache 在处理我的请求时采取了哪些步骤?当它尝试查找匹配的 VirtualHost 时,它是否在任何地方记录了它所使用的 ServerName?

更新:

我为我的第二个网站的子域添加了 VirtualHost 部分,并且成功了。当我在浏览器中输入“interface.domain.com”时,它会从在 8501 端口运行的正确应用程序服务器呈现页面。但是当我尝试“www.domain.com”时,它会重定向到我的第一个网站。

下面是我的第二个网站的虚拟主机文件。

<VirtualHost *:80>
  ServerName www.domain.com
  DocumentRoot /home/domain/current/public

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ http://localhost:8501/$1 [P]
</VirtualHost>

<VirtualHost *:80>
  ServerName interface.domain.com
  DocumentRoot /home/domain/current/public

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ http://localhost:8501/$1 [P]
</VirtualHost>

apache2ctl -S

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 1.2.3.4. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
1.2.3.4:443        www.domainfirst.com (/etc/apache2/sites-enabled/https_www_domainfirst_com.conf:1)
1.2.3.4:80         is a NameVirtualHost
         default server domainfirst.com (/etc/apache2/sites-enabled/domainfirst_com.conf:1)
         port 80 namevhost domainfirst.com (/etc/apache2/sites-enabled/domainfirst_com.conf:1)
         port 80 namevhost www.domainfirst.com (/etc/apache2/sites-enabled/www_domainfirst_com.conf:1)
*:80                   is a NameVirtualHost
         default server domain.com (/etc/apache2/sites-enabled/domain_com.conf:1)
         port 80 namevhost domain.com (/etc/apache2/sites-enabled/domain_com.conf:1)
         port 80 namevhost www.domain.com (/etc/apache2/sites-enabled/www_domain_com.conf:1)
         port 80 namevhost interface.domain.com (/etc/apache2/sites-enabled/www_domain_com.conf:10)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl 
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

解决方案:

清除浏览器缓存。我的浏览器缓存了一条将请求转发到其他网站的规则。

答案1

不是在您的标签中混合 IP:port 和 *:port 定义,<VirtualHost>除非您是一位非常有经验的 Apache 管理员,并且确切知道自己在做什么。

1.2.3.4无论使用哪个主机名,任何解析为(在您的示例中)的域都将由绑定到 1.2.3.4:80 的域提供服务。

如果您的 *:80 虚拟主机中的服务器名称和服务器别名不要解析到 1.2.3.4,则说明其他问题。如果他们确实解析到这个 IP,只需将您更改<VirtualHost 1.2.3.4:80><Virtualhost *:80>

补充说明:如果你的服务器上只有 1 个 IP,请仅使用 *:port 版本,这样可以避免混淆

更多评论:部分问题可能在于你清理所有域名的方式。这必须必须始终如一地完成,否则我们的努力将付诸东流。Apache 决定将请求发送到哪个服务器的方式如下。这很简单,任何漏洞在这个过程中,甚至无法通过 Apache 版本的最基本的测试:

  • 如果连接到特定的 IP 地址,并且 apache 具有<VirtualHost>与该 ip/端口匹配的单个标签,则该虚拟主机将接收该请求
  • 如果不是,apache 会查看ServerName和指令,查找与连接所用的 IP/端口匹配的指令,以及与请求附带的标头匹配的指令ServerAlias。输出中第一个匹配的指令将成为接收请求的指令。<VirtualHost>HTTP Host<VirtualHost>apachectl -S
  • 如果未找到匹配项,则该 IP/端口的默认虚拟主机将接收请求。这是apachectl -S特定 IP/端口组合的输出中列出的第一个虚拟主机。

记住知识产权可能只是*

相关内容