Apache 虚拟主机:HTTP 被重定向到默认的 Apache 站点

Posted

技术标签:

【中文标题】Apache 虚拟主机:HTTP 被重定向到默认的 Apache 站点【英文标题】:Apache Virtual Host: HTTP getting redirected to the default Apache site 【发布时间】:2017-06-04 10:42:56 【问题描述】:

我已经使用 Apache 虚拟服务器在 VPS 上托管了一个网站,因为我计划稍后在该 VPS 上托管更多网站。在我使用 LetsEncrypt Certbot 在其上安装 SSL 之前,虚拟主机一直正常工作。之后,https://techkernel.org 工作正常,但 http 变体 http://techkernel.org 被重定向到默认的 Apache 网页。虚拟主机配置无法正常工作,它被定向到默认的 /var/www/html 目录。

如果您能告诉我如何修复它,以便将 HTTP 和 HTTPS 请求都重定向到 HTTPS 并显示正确的网站,那将非常有帮助。谢谢。

相关信息:

etc/apache2/sites-available/techkernel.org.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.techkernel.org

    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/techkernel.org/public"

    ServerAlias techkernel.org

    # 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
    RewriteEngine on
    RewriteCond %SERVER_NAME =www.techkernel.org [OR]
    RewriteCond %SERVER_NAME =techkernel.org
    RewriteRule ^ https://%SERVER_NAME%REQUEST_URI [END,QSA,R=permanent]

</VirtualHost>

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

var/www/techkernel.org/public/.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %HTTPS off
RewriteRule (.*) https://%SERVER_NAME/%$1 [R,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

ls -l /etc/apache2/sites-enabled

lrwxrwxrwx 1 root root 35 Dec 28 11:08 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 38 Jan 21 04:31 techkernel.org.conf -> ../sites-available/techkernel.org.conf
lrwxrwxrwx 1 root root 55 Dec 28 19:46 techkernel.org-le-ssl.conf -> /etc/apache2/sites-available/techkernel.org-le-ssl.conf

ls -l /etc/apache2/sites-enabled

-rw-r--r-- 1 root root 1332 Jul  5  2016 000-default.conf                                                                                                           
-rw-r--r-- 1 root root 6437 Aug  7 10:56 default-ssl.conf                                                                                                           
-rw-r--r-- 1 root root 1574 Jan 19 16:05 techkernel.org.conf                                                                                                        
-rw-r--r-- 1 root root 1613 Dec 28 19:46 techkernel.org-le-ssl.conf

【问题讨论】:

【参考方案1】:

除了您可能在 000-default.conf 中发现的错误之外,您的重写规则中还存在以下问题:

RewriteEngine on
RewriteCond %SERVER_NAME =www.techkernel.org [OR]
RewriteCond %SERVER_NAME =techkernel.org
RewriteRule ^ https://%SERVER_NAME%REQUEST_URI [END,QSA,R=permanent]

应该是

RewriteEngine on
RewriteCond %HTTP_HOST ^(www\.)?techkernel.org
RewriteRule ^/(.*) https://%HTTP_HOST/$1 [NC,R=301,L]

%SERVER_NAME 可能是来自 VirtualHost 配置的值,如果设置了 UseCanonicalName On。因此,无论访问者使用什么 URI,它都会重定向所有内容。如果这是意图,那么只需删除任何 RewriteCond 并使用

RewriteEngine on
RewriteRule ^/(.*) https://www.techkernel.org/$1 [NC,R=301,L]

如果已解决,您可以/应该在您的 .htaccess 中删除所有与此相关的条目

RewriteCond %HTTPS off
RewriteRule (.*) https://%SERVER_NAME/%$1 [R,L]

【讨论】:

【参考方案2】:

您已将配置粘贴为站点可用的示例。您是否已将其符号链接到站点 - 已启用以实际启用它?

编辑 - 抱歉,这似乎是不必要的。我的观点是您的 HTTPS 变体正在工作,并且位于单独的配置文件中。您的 HTTP 不是,这在我看来是因为您尚未创建此符号链接,因此 Apache 没有配置“当您在端口 80 上收到主机的请求时您会做什么:标头设置为 techkernel.org”,因此它是掉到默认站点。你的配置对我来说看起来非常好,它实际上并没有被 Apache 使用。

【讨论】:

我是新手。如何创建符号链接? @SkullTech a2ensite techkernel.org @scampbell 这似乎不起作用。首先它说Site techkernel.org already enabled。所以我a2dissite'd 它然后a2ensite'd 再次。并重新加载apache2。但还是没有结果。 edit - 哎呀,我不记得 SO 格式是如何工作的...听起来你有两个不同的 文件,而不是 symlinks . “ls -l /etc/apache2/sites-available,enabled”是什么意思?听起来您有两个文件,都称为“techkernel.org.conf”,一个可用(配置为 HTTP),一个已启用(配置为 HTTPS)。因此,a2ensite 认为您正在尝试启用已启用的配置。 我可能会做的(除了我在那条评论中没有空间)是:` mv /etc/apache2/sites-enabled/techkernel.org.conf /etc/apache2/sites- available/HTTPS_techkernel.org.conf mv /etc/apache2/sites-available/techkernel.org.conf /etc/apache2/sites-available/HTTP_techkernel.org.conf a2ensite HTTP_techkernel.org a2ensite HTTPS_techkernel.org service apache2 reload `系统Apache 使用的设计非常相似,因此 site,conf,mods-available 包含所有带有设置的真实文件,然后将这些文件符号链接到 -enabled 以打开它们【参考方案3】:

原来 000-default.conf 搞砸了。只有在运行apachectl -S 命令后我才注意到它。修复它(输入正确的ServerNameDocumentRoot 以及一些URL 重写条件)和reloading apache2 后,它可以完美运行。

【讨论】:

以上是关于Apache 虚拟主机:HTTP 被重定向到默认的 Apache 站点的主要内容,如果未能解决你的问题,请参考以下文章

为啥在 Apache 中设置虚拟主机后,http://localhost 会重定向到我的默认虚拟主机?

Apache 默认为一个虚拟主机,而不是其他虚拟主机的根文件夹

Apache:在服务器名称不匹配时禁用重定向到默认虚拟主机

Apache vhost 重定向到另一个 vhost

在没有 ServerName 的默认虚拟主机上将 HTTP 重定向到 HTTPS

使用 apache vhost 配置重定向到新域