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

介绍

Apache 是世界上最受欢迎的 Web 服务器,它具有良好的支持、功能丰富和灵活性. 当设计您的 Web 页面时,通常有助于定制用户看到的每个内容。

前提条件

要开始使用本指南,您将需要具有sudo特权的非根用户。您可以通过遵循我们的 CentOS 7 的初始设置指南来设置此类用户。

创建自己的错误页面

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

我们会将我们的自定义错误页放入‘/var/www/html’目录中,在那里CentOS的Apache安装设置了其默认文件根。我们会创建一个名为‘custom_404.html’的404错误页面,一个名为‘custom_50x.html’的一般500级错误页面。如果您只是在测试,您可以使用下列行。

1echo "<h1 style='color:red'>Error 404: Not found :-(</h1>" | sudo tee /var/www/html/custom_404.html
2echo "<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
3echo "<h1>Oops! Something went wrong...</h1>" | sudo tee /var/www/html/custom_50x.html
4echo "<p>We seem to be having some technical difficulties. Hang tight.</p>" | sudo tee -a /var/www/html/custom_50x.html

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

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

现在,我们只需要告诉Apache,当出现正确的错误条件时,它应该使用这些页面。我们可以在/etc/httpd/conf.d目录中创建一个新的配置文件,其中Apache会读 config snippets。

1sudo nano /etc/httpd/conf.d/custom_errors.conf

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

直接错误到正确的自定义页面

我们可以使用ErrorDocument指令将每个类型的错误与相关错误页面相关联。

对于我们的例子,错误地图将看起来像这样:

1[label /etc/httpd/conf.d/custom_errors.conf]
2ErrorDocument 404 /custom_404.html
3ErrorDocument 500 /custom_50x.html
4ErrorDocument 502 /custom_50x.html
5ErrorDocument 503 /custom_50x.html
6ErrorDocument 504 /custom_50x.html

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

然而,我们将添加一组额外的配置,以便我们的错误页面不能直接由客户端请求,这可以防止一些奇怪的情况,其中页面的文本引用错误,但HTTP状态是200(表示成功的请求)。

404 当错误页面被直接请求时

要实现这种行为,我们需要为每个自定义页面添加一个文件块。内部,我们可以测试是否设置了REDIRECT_STATUS环境变量。

 1[label /etc/httpd/conf.d/custom_errors.conf]
 2ErrorDocument 404 /custom_404.html
 3ErrorDocument 500 /custom_50x.html
 4ErrorDocument 502 /custom_50x.html
 5ErrorDocument 503 /custom_50x.html
 6ErrorDocument 504 /custom_50x.html
 7
 8<Files "custom_404.html">
 9    <If "-z %{ENV:REDIRECT_STATUS}">
10        RedirectMatch 404 ^/custom_404.html$
11    </If>
12</Files>
13
14<Files "custom_50x.html">
15    <If "-z %{ENV:REDIRECT_STATUS}">
16        RedirectMatch 404 ^/custom_50x.html$
17    </If>
18</Files>

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

设置500级错误测试

我们可以轻松地生成404错误来测试我们的配置,通过请求不存在的内容。

在文件底部添加一个ProxyPass指令,向本地机器的端口9000发送/proxytest请求(没有服务):

 1[label /etc/httpd/conf.d/custom_errors.conf]
 2ErrorDocument 404 /custom_404.html
 3ErrorDocument 500 /custom_50x.html
 4ErrorDocument 502 /custom_50x.html
 5ErrorDocument 503 /custom_50x.html
 6ErrorDocument 504 /custom_50x.html
 7
 8<Files "custom_404.html">
 9    <If "-z %{ENV:REDIRECT_STATUS}">
10        RedirectMatch 404 ^/custom_404.html$
11    </If>
12</Files>
13
14<Files "custom_50x.html">
15    <If "-z %{ENV:REDIRECT_STATUS}">
16        RedirectMatch 404 ^/custom_50x.html$
17    </If>
18</Files>
19
20ProxyPass /proxytest "http://localhost:9000"

保存并关闭文件,当你完成。

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

通过键入测试您的配置文件的语法错误:

1sudo apachectl configtest

当您的文件不包含语法错误时,请通过键入重新启动 Apache:

1sudo systemctl restart httpd

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

1http://server_domain_or_IP/thiswillerror

apache custom 404

当您到我们为虚假代理传输设置的位置时,我们会在我们的自定义500级页面中收到503服务不可用错误:

1http://server_domain_or_IP/proxytest

apache custom 50x

您现在可以回去并从您的 Apache 配置中删除假的代理传输行。

结论

您现在应该为您的网站提供自定义错误页面,这是一个简单的方式来个性化用户的体验,即使他们遇到问题。 这些页面的一个建议是包括链接到他们可以寻求帮助或更多信息的地方。

Published At
Categories with 技术
Tagged with
comments powered by Disqus