Apache虚拟主机无法正常工作-我的网站已重定向到默认虚拟主机

时间:2018-12-19 06:54:51

标签: apache virtualhost ubuntu-18.04

我正在使用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://wwww.speedysoftware.com/http://speedysoftware.com/都指向正确的虚拟主机。只有http://www.speedysoftware.com/不会导致正确的虚拟主机。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!我写信给DigitalOcean支持(顺便说一句,他们得到了很大的支持),他们写信给我:

“服务器是Apache,因此您可以访问apachectl命令,该命令支持-S标志以获取转储的分析设置,其中包括在其中建立虚拟主机和配置的信息。”

我从命令行运行了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添加到了此文件:

<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.speedy-soft.com

    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

然后重新启动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)
         ...

现在一切正常。谢谢!

您可以在此页面上查看更多详细信息: How to debug an apache virtual host configuration?