Apache-2.4

Apache 虛擬主機未按預期工作 - 我的網站被重定向到預設虛擬主機

  • September 12, 2020

我正在使用 Ubuntu 18.04.1 LTS 配置新伺服器(droplet)。我安裝了 apache 並配置了虛擬主機。伺服器名稱是www.speedysoftware.com,這是 的值/etc/hostname。問題是,我試圖將http://www.speedysoftware.com/重定向到特定的虛擬主機,但它被重定向到預設的虛擬主機。另一方面,http ://www.speedysoft.com/和http://www.speedy-software.com/被重定向到正確的虛擬主機,應該與http://www.speedysoftware 相同。 com/

文件/etc/apache2/sites-available/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.
   ServerAdmin webmaster@speedy.net
   DocumentRoot /var/www/general

   <Directory /var/www/general>
       Options +FollowSymLinks
       Options -Indexes
       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>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

文件/etc/apache2/sites-available/speedysoftware.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.speedysoftware.com
   ServerAlias speedysoftware.com *.speedysoftware.com
   ServerAlias speedy-software.com *.speedy-software.com
   ServerAlias speedysoft.com *.speedysoft.com
   # ... (more ServerAliases)

   ServerAdmin webmaster@speedy.net
   DocumentRoot /var/www/speedysoftware

   <Directory /var/www/speedysoftware>
       Options +FollowSymLinks
       Options -Indexes
       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>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

可以看到http://www.speedy-soft.com/被重定向到預設的虛擬主機,這是正確的。配置有什麼問題?

進行更改後,我重新載入了設置sudo systemctl restart apache2

關於重定向,在這種情況下我不想重定向。我希望它們被視為獨立的網站,儘管它們指向同一個虛擬主機。在其他情況下,我會重定向,例如我將http://www.speedypedia.org/>重定向到<http://www.speedypedia.info/>,然後重定向<http://ww.speedysoftware.com/>(或任何其他子域,包括<http://speedysoftware.com/>)到<http://www.speedysoftware.com/

更新- 我檢查了http://ww.speedysoftware.com/>、[http://www.speedysoftware.com/](http://wwww.speedysoftware.com/)和<http://speedysoftware.com/>都指向正確的虛擬主機。只有<http://www.speedysoftware.com/不會導致正確的虛擬主機。

我寫信給 DigitalOcean 支持(順便說一句,他們有很大的支持),他們寫信給我:

伺服器是 Apache,因此您可以訪問支持 -S 標誌的 apachectl 命令以獲取轉儲的已解析設置,其中包括建立它們的虛擬主機和配置。

apachectl -S從命令行執行並發現了問題:

*:80                   is a NameVirtualHost
        default server www.speedysoftware.com (/etc/apache2/sites-enabled/000-default.conf:1)
        port 80 namevhost www.speedysoftware.com (/etc/apache2/sites-enabled/000-default.conf:1)
        ...

由於我沒有在 中定義ServerName/etc/apache2/sites-available/000-default.conf因此採用了液滴名稱,這就是問題所在。所以我添加ServerName www.speedy-soft.com到這個文件中:

&lt;VirtualHost *:80&gt;
   # 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.speedy-soft.com

   ServerAdmin webmaster@speedy.net
   DocumentRoot /var/www/general

   &lt;Directory /var/www/general&gt;
       Options +FollowSymLinks
       Options -Indexes
       AllowOverride All
       Require all granted
   &lt;/Directory&gt;

   # 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
&lt;/VirtualHost&gt;

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

並重新啟動 Apache。我又跑apachectl -S了:

*:80                   is a NameVirtualHost
        default server www.speedy-soft.com (/etc/apache2/sites-enabled/000-default.conf:1)
        port 80 namevhost www.speedy-soft.com (/etc/apache2/sites-enabled/000-default.conf:1)
        ...

現在一切正常。

您可以在此頁面上查看更多詳細資訊: https ://stackoverflow.com/questions/5474477/how-to-debug-an-apache-virtual-host-configuration

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