Apache vhost加载不正确的doc root

时间:2016-10-26 17:55:38

标签: apache ubuntu

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

背景:

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

服务器怪异?

  • 我似乎无法阻止apache。执行service apache2 stop然后service apache2 status会将进程标识为非活动状态,但我仍然可以在domain1 / 2 / 3.com下加载页面。这就像我所做的一切都没有任何效果。
  • 现在有三种方法可以访问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

3 个答案:

答案 0 :(得分:2)

就Apache选择随机网站而言:

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

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

最好将000-default.conf设置为catch-all并决定在发生这种情况时该怎么做,比如重定向到你的某个站点或者只是回味一些简单的php文件。然后为每个站点设置单独的conf文件,以保持井井有条。

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

答案 1 :(得分:0)

你的配置看起来没问题,你似乎描述的怪异似乎与外部问题有关,无论是发行脚本还是其他......,所以尽量只使用apachectl停止/启动,如果发行版没有& #39; t改变它是最多&#34;官方&#34;您将找到管理您的httpd服务器启动/停止等的方法,此外它是您现在唯一能为您提供正确信息的方法

此外,在您的配置中,domain3.com永远不会是您的默认虚拟主机,即使您错误地定义了服务器名称也是如此。所以很有可能你会看到其他什么&#34; service / server&#34;送你。

答案:

Use apachectl only until you fix your discro script issues

答案 2 :(得分:0)

在Ubuntu 16.04上你有systemd ..所以与apache交互的正确方法是使用systemctl <command> apache2

我建议杀死每个apache进程,然后尝试使用systemctl start apache2再次启动 ..

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