将虚拟主机添加到 Apache 可防止访问本地主机

将虚拟主机添加到 Apache 可防止访问本地主机

我在本地个人笔记本电脑上使用 Uubntu 16.04 和 Apache2(版本 2.4)进行开发。我在 sites-enabled 目录下添加了一个虚拟主机(呵呵)。

自从我添加了它之后,我就无法访问http://本地主机来自 Firefox,但不是 Chrome。

在 Chrome 中,访问http://本地主机我正确获取了根 Web 目录,并且http://呵呵将我带到新的虚拟主机目录。

在 Firefox 中访问http://本地主机还将我带到虚拟主机的 /hehe 目录。

附件是两个文件。很奇怪,但这肯定是我的虚拟主机配置出了问题。

000-默认.conf

<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.example.com

ServerAdmin webmaster@localhost
DocumentRoot /home/xxx/myprojects/web

# 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>
<Directory /home/xxx/myprojects/web/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order deny,allow
        #Allow from all
    Require ip 127.0.0.1
    Require ip ::1
</Directory>

001-呵呵.conf

NameVirtualHost hehe:80
<VirtualHost hehe: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 hehe

    ServerAdmin webmaster@localhost
    DocumentRoot /home/xxx/myprojects/web/understandmydreams

    # 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>
<Directory /home/xxx/myprojects/web/understandmydreams/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order deny,allow
        #Allow from all
    Require ip 127.0.0.1
    Require ip ::1
</Directory>

知道如何才能拥有本地主机和虚拟主机??

答案1

不要这样做:

NameVirtualHost hehe:80
<VirtualHost hehe:80>

它不会按照您的意图执行操作,但可能会在将来重新启动 Apache 时导致一些棘手的问题(它引入了对名称解析的依赖)。由于人为的暗示,它很少被使用。

您可以并且应该声明重复子句:

NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost

其次是

NameVirtualHost *:80
<VirtualHost *:80>
ServerName hehe

答案2

尝试启用 000-default.conf 中指向 localhost 的“ServerName”指令。

正如解释的那样这里最好始终在每个基于名称的虚拟主机中明确列出 ServerName。

相关内容