Apache-2.4

將虛擬主機添加到 Apache 以防止訪問 localhost

  • November 5, 2019

我在本地個人筆記型電腦上使用 Uubntu 16.04 和 Apache2(2.4 版)進行開發。我在啟用站點的目錄下添加了一個虛擬主機(呵呵)。

由於我添加了它,我無法從 Firefox 而不是 Chrome訪問http://localhost 。

在 Chrome 中,當訪問http://localhost時,我正確獲取了根 Web 目錄,而http://hehe將我帶到了新的虛擬主機目錄。

在 Firefox 中訪問http://localhost也將我帶到虛擬主機的 /hehe 目錄。

附上這兩個文件。很奇怪,但它一定是我的虛擬主機配置中的東西。

000-default.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>

知道如何同時擁有localhost 和虛擬主機嗎?

不要這樣做:

NameVirtualHost hehe:80
<VirtualHost hehe:80>

它沒有做你想做的事情,但可能會在未來的 apache 重新啟動時導致嚴重的問題(它引入了對名稱解析的依賴)。由於人為的含義,它很少使用。

您可以並且應該聲明一個重複的子句:

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

其次是

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

嘗試在 000-default.conf 中啟用指向 localhost 的“ServerName”指令。

正如此處所解釋的,最好始終在每個基於名稱的虛擬主機中明確列出一個 ServerName。

引用自:https://serverfault.com/questions/990534