【问题标题】:Apache vhost loads incorrect doc rootApache vhost 加载不正确的文档根目录
【发布时间】:2017-03-09 05:18:26
【问题描述】:

我的问题是 Apache 随机加载一个非默认虚拟主机而不是我要求它加载的域。我已经尝试了在这个网站上找到的几乎所有解决方案,并通过搜索,但似乎没有任何解决方案可以解决这个问题。

背景:

  • 这是托管在流行云平台上的虚拟服务器
  • 全局 apache conf 文件不包含 VirtualHost 定义,并且正确包含以下配置文件(包括 sites-enabled/*.conf)
  • Ubuntu 16.04 上的 Apache 2.4
  • 我将每个域都放在自己的 conf 文件中,但这也不起作用。
  • Apache 出于某种原因选择了 domain3.com 作为默认值。
  • 当我在服务器上执行curl localhost 时,它会返回 domain3.com 的内容。
  • 唯一从以下虚拟主机中排除的是一些似乎不会导致任何问题的 SSL 定义。
  • ServerName 在 apache.conf 中定义为 localhost
  • 下面的虚拟主机保存在/etc/apache2/sites-enabled/000-default.conf

服务器怪异?

  • 我似乎无法真正停止 apache。执行service apache2 stop,然后执行service apache2 status 会将进程识别为非活动状态,但我仍然可以加载 domain1/2/3.com 下的页面。好像我做的任何事情都没有任何效果。
  • 现在有 3 种访问 apache 的方法。有些似乎有效,有些则无效。 /etc/init.d/apache2service apache2apache2ctl
  • 只有大约 50% 的时间加载错误的网站。

我完全没有想法......

NameVirtualHost *:80

# domain1.com
<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.domain1.com
    ServerAlias domain1.com
    UseCanonicalName Off
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/domain1.com/

    <Directory /var/www/domain1.com>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# domain3.com
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName www.domain3.com
        ServerAlias domain3.com
        ServerAlias domain3.net
        ServerAlias www.domain3.net
        UseCanonicalName Off
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/domain3.com/

        <Directory /var/www/domain3.com>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/cq2_error.log
        CustomLog ${APACHE_LOG_DIR}/cq2_access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# domain2.com
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName www.domain2.com
        ServerAlias domain2.com
        UseCanonicalName Off
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/domain2.com/

        <Directory /var/www/domain2.com>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

还有我的apache2ctl -S 输出:

AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:1
VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server www.domain1.com (/etc/apache2/sites-enabled/000-default.conf:4)
         port 80 namevhost www.domain1.com (/etc/apache2/sites-enabled/000-default.conf:4)
                 alias domain1.com
         port 80 namevhost www.domain3.com (/etc/apache2/sites-enabled/000-default.conf:54)
                 alias domain3.com
                 alias domain3.net
                 alias www.domain3.net
         port 80 namevhost www.domain2.com (/etc/apache2/sites-enabled/000-default.conf:95)
                 alias domain2.com
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl 
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used

【问题讨论】:

    标签: apache ubuntu


    【解决方案1】:

    至于 Apache 选择一个随机站点:

    当您执行curl localhost 时,它可能会发送一个未定义Host: 的HTTP 标头,因为它的行为类似于您直接在浏览器中键入服务器的IP。 HTTP 请求中的Host: 标头是Apache 与ServerNameServerAlias 匹配的字段,它试图找到匹配项以加载相应的DocumentRoot

    因此,在这种情况下,如果您没有默认主机设置,并且有一个空的 Host: 进入,那么 Apache 将随机选择一个站点(我在我的开发服务器上重新创建了此行为,只是为了验证) .

    最好将 000-default.conf 设置为您的包罗万象,并在发生这种情况时决定该怎么做,例如重定向到您的一个站点或一个简单的 php 文件,它只是回显一些平淡无奇的东西。然后为每个站点设置单独的 conf 文件以保持井井有条。

    如果您的 DNS 记录正确指向您的服务器,则默认设置只会在机器人出现测试 IP 时触发。

    【讨论】:

      【解决方案2】:

      在 Ubuntu 16.04 上,您有 systemd .. 所以与 apache 交互的正确方法是使用 systemctl &lt;command&gt; apache2

      我建议杀死所有 apache 进程,然后尝试使用 systemctl start apache2 重新启动它。..

      此外,为了确保首先加载我的默认域,我通常将其配置文件重命名为 000-domain.conf .. 然后添加其他域,每个文件一个,根本没有数字前缀

      【讨论】:

      • 只是想在这里说清楚,你不会把它命名为000-default.conf?但是000-apple.conf 如果域是http://apple.com
      • 这个确实解决了我的问题。由于僵尸进程,apache 似乎没有完全重新启动。清除旧的 apache 进程导致配置正确重新加载,现在一切正常。谢谢!
      【解决方案3】:

      您的配置看起来不错,您似乎描述的怪异似乎与外部问题有关,无论是发行版脚本还是其他...,因此请尝试仅使用 apachectl 来停止/启动,如果发行版没有'没有太大改变是您会发现管理您的 httpd 服务器启动/停止等的最“官方”方法,而且它是您现在唯一可以为您提供正确信息的方法

      此外,在您的配置中 domain3.com 永远不会是您的默认虚拟主机,即使您错误地定义了服务器名称也不会。因此,您很可能会看到其他“服务/服务器”正在向您发送什么。

      答案:

      Use apachectl only until you fix your discro script issues
      

      【讨论】:

      • 我知道,这就是它如此奇怪的原因。
      猜你喜欢
      • 2020-09-24
      • 2012-05-03
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 2015-01-28
      • 2013-05-06
      • 2016-06-21
      相关资源
      最近更新 更多