gpt4 book ai didi

Apache vhost 加载不正确的文档根目录

转载 作者:行者123 更新时间:2023-12-04 18:41:01 28 4
gpt4 key购买 nike

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

背景:

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

  • 服务器怪异?
  • 我似乎无法真正停止 apache。执行service apache2 stop然后 service apache2 status会将进程识别为非事件状态,但我仍然可以加载 domain1/2/3.com 下的页面。就像我所做的任何事情都没有任何效果一样。
  • 现在有 3 种访问 apache 的方法。有些似乎有效,有些则无效。 /etc/init.d/apache2 , service apache2 , apache2ctl .
  • 它仅在大约 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 选择一个随机站点:

    当你这样做 curl localhost ,它很可能发送了一个没有 Host: 的 HTTP header 定义,因为它的行为类似于直接在浏览器中键入服务器的 IP。 Host: HTTP 请求中的 header 是 Apache 与 ServerName 匹配的字段。和 ServerAlias试图找到一个匹配来加载相应的DocumentRoot .

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

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

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

    关于Apache vhost 加载不正确的文档根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40269078/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com