如何配置 Apache 以在 Ubuntu 14.04 上使用自定义错误页面如何配置 Apache 以在 Ubuntu 14.04 上使用自定义错误页面如何配置 Apache 以在 Ubuntu 14.04 上使用自定义错误页面如何配置 Apache 以在 Ubuntu 14.04 上使用自定义错误页面
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何配置 Apache 以在 Ubuntu 14.04 上使用自定义错误页面

介绍

Apache 是世界上最流行的 Web 服务器。它得到了很好的支持,功能丰富且灵活。在设计网页时,自定义用户将看到的每一条内容通常很有帮助。这包括当他们请求不可用的内容时的错误页面。在本指南中,我们将演示如何配置 Apache 以在 Ubuntu 14.04 上使用自定义错误页面。

先决条件

要开始使用本指南,您需要一个具有 sudo 权限的非根用户。您可以按照我们的本指南设置此类用户。

创建您的自定义错误页面

我们将创建一些自定义错误页面用于演示目的,但您的自定义页面显然会有所不同。

我们将把我们的自定义错误页面放在 /var/www/html 目录中,Ubuntu 的 Apache 安装设置了它的默认文档根目录。我们将为 404 错误创建一个名为 custom_404.html 的页面,为一般 500 级错误创建一个名为 custom_50x.html 的页面。如果您只是测试,可以使用以下行。否则,将您自己的内容放在这些位置:

  1. echo "<h1 style='color:red'>Error 404: Not found :-(</h1>" | sudo tee /var/www/html/custom_404.html
  2. echo "<p>I have no idea where that file is, sorry. Are you sure you typed in the correct URL?</p>" | sudo tee -a /var/www/html/custom_404.html
  3. echo "<h1>Oops! Something went wrong...</h1>" | sudo tee /var/www/html/custom_50x.html
  4. echo "<p>We seem to be having some technical difficulties. Hang tight.</p>" | sudo tee -a /var/www/html/custom_50x.html

我们现在有两个自定义错误页面,当客户端请求导致不同的错误时,我们可以提供它们。

配置 Apache 以使用您的错误页面

现在,我们只需要告诉 Apache 它应该在正确的错误条件出现时使用这些页面。在 /etc/apache2/sites-enabled 目录中打开您要配置的虚拟主机文件。我们将使用名为 000-default.conf 的默认服务器块文件,但如果您使用的是非默认文件,则应调整自己的服务器块:

  1. sudo nano /etc/apache2/sites-enabled/000-default.conf

我们现在可以将 Apache 指向我们的自定义错误页面。

将错误直接指向正确的自定义页面

我们可以使用 ErrorDocument 指令将每种类型的错误与关联的错误页面相关联。这可以在当前定义的虚拟主机中设置。基本上,我们只需要将每个错误的 http 状态代码映射到我们希望在其发生时提供服务的页面。

对于我们的示例,错误映射将如下所示:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    ErrorDocument 404 /custom_404.html
    ErrorDocument 500 /custom_50x.html
    ErrorDocument 502 /custom_50x.html
    ErrorDocument 503 /custom_50x.html
    ErrorDocument 504 /custom_50x.html
</VirtualHost>

仅此更改就足以在发生指定错误时为自定义错误页面提供服务。

但是,我们将添加一组额外的配置,以便客户无法直接请求我们的错误页面。这样可以防止一些奇怪的情况,页面的文本引用错误,但是http状态是“200”(表示请求成功)。

直接请求错误页面时响应 404

要实现此行为,我们需要为每个自定义页面添加一个 Files 块。在内部,我们可以测试是否设置了 REDIRECT_STATUS 环境变量。这应该只在 ErrorDocument 指令处理请求时设置。如果环境变量为空,我们将提供 404 错误:

<VirtualHost *:80>

    . . .

    ErrorDocument 404 /custom_404.html
    ErrorDocument 500 /custom_50x.html
    ErrorDocument 502 /custom_50x.html
    ErrorDocument 503 /custom_50x.html
    ErrorDocument 504 /custom_50x.html

    <Files "custom_404.html">
        <If "-z %{ENV:REDIRECT_STATUS}">
            RedirectMatch 404 ^/custom_404.html$
        </If>
    </Files>

    <Files "custom_50x.html">
        <If "-z %{ENV:REDIRECT_STATUS}">
            RedirectMatch 404 ^/custom_50x.html$
        </If>
    </Files>
</VirtualHost>

当客户端直接请求错误页面时,会出现404错误,因为没有设置正确的环境变量。

设置 500 级错误测试

我们可以通过请求不存在的内容轻松产生 404 错误来测试我们的配置。为了测试 500 级错误,我们必须设置一个虚拟代理通道,以便我们可以确保返回正确的页面。

将 ProxyPass 指令添加到虚拟主机的底部。将对 /proxytest 的请求发送到本地机器上的端口 9000(没有服务正在运行):

<VirtualHost *:80>

    . . .

    ErrorDocument 404 /custom_404.html
    ErrorDocument 500 /custom_50x.html
    ErrorDocument 502 /custom_50x.html
    ErrorDocument 503 /custom_50x.html
    ErrorDocument 504 /custom_50x.html

    <Files "custom_404.html">
        <If "-z %{ENV:REDIRECT_STATUS}">
            RedirectMatch 404 ^/custom_404.html$
        </If>
    </Files>

    <Files "custom_50x.html">
        <If "-z %{ENV:REDIRECT_STATUS}">
            RedirectMatch 404 ^/custom_50x.html$
        </If>
    </Files>

    ProxyPass /proxytest "http://localhost:9000"
</VirtualHost>

完成后保存并关闭文件。

现在,通过键入以下命令启用 mod_proxy 和 mod_proxy_http 模块:

  1. sudo a2enmod proxy
  2. sudo a2enmod proxy_http

重新启动 Apache 并测试您的页面

键入以下命令测试配置文件是否存在语法错误:

  1. sudo apache2ctl configtest

解决报告的任何问题。当您的文件不包含语法错误时,通过键入以下命令重新启动 Apache:

  1. sudo service apache2 restart

现在,当您转到服务器的域或 IP 地址并请求一个不存在的文件时,您应该会看到我们设置的 404 页面:

http://server_domain_or_IP/thiswillerror

当您前往我们为虚拟代理通行证设置的位置时,我们将在我们的自定义 500 级页面中收到“503 服务不可用”错误:

http://server_domain_or_IP/proxytest

您现在可以返回并从 Apache 配置中删除伪造的代理传递行。如果您不需要在其他地方使用代理模块,您可以禁用它们:

  1. sudo a2dismod proxy
  2. sudo a2dismod proxy_http

再次重新启动服务器以实施这些更改:

  1. sudo service apache2 restart

结论

您现在应该为您的站点提供自定义错误页面。这是一种个性化用户体验的简单方法,即使他们遇到问题也是如此。对这些页面的一个建议是包括指向他们可以去获取帮助或更多信息的位置的链接。如果这样做,请确保即使在发生相关错误时也可以访问链接目标。

©2015-2025 艾丽卡 support@alaica.com