Apache是世界上最受欢迎的Web服务器。它功能强大,功能丰富且灵活。在设计网页时,自定义用户看到的每条内容通常都很有帮助。包括他们请求不可用内容时的错误页面。在本教程中,我们将演示如何配置Apache以在CentOS 7上使用自定义错误页面。
要开始使用本教程,您需要具有一台可以使用sudo
权限的非root账号的CentOS服务器,并且已开启防火墙。没有服务器的同学可以在这里购买,不过我个人更推荐您使用免费的腾讯云开发者实验室进行试验,学会安装后再购买服务器。您还需要在系统上安装Apache。按照本教程的第一步开始学习如何进行设置。
我们将为了演示创建一些自定义错误页面,但您的自定义页面显然会有所不同。
我们将自定义错误页面放在CentOS的Apache安装设置其默认文档根目录的/var/www/html
目录中。我们将为404错误调用一个custom_404.html
页面,调用一个500级错误页面custom_50x.html
。如果您只是测试,可以使用以下行。或者将您自己的内容放在以下位置:
echo "<h1 style='color:red'>Error 404: Not found :-(</h1>" | sudo tee /var/www/html/custom_404.html
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
echo "<h1>Oops! Something went wrong...</h1>" | sudo tee /var/www/html/custom_50x.html
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/httpd/conf.d
。我们将调用新文件custom_errors.conf
:
sudo nano /etc/httpd/conf.d/custom_errors.conf
我们现在可以将Apache指向我们的自定义错误页面。
我们可以使用该ErrorDocument
指令将每种类型的错误与关联的错误页面相关联。基本上,我们只需将每个错误的http状态代码映射到我们想要在其发生时提供的页面。
对于我们的示例,映射将如下所示:
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
仅发生此更改就足以在发生指定错误时提供自定义错误页面。
但是,我们将添加一组额外的配置,以便客户端无法直接请求我们的错误页面。这可以防止页面文本引用错误的某些奇怪情况,但http状态为“200”(表示请求成功)。
要实现此行为,我们需要为每个自定义页面添加一个Files
块。在里面,我们可以测试是否设置了环境变量REDIRECT_STATUS
。只应在ErrorDocument
指令处理请求时设置此项。如果环境变量为空,我们将发出404错误:
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>
当客户端直接请求错误页面时,将发生404错误,因为未设置正确的环境变量。
我们可以通过请求不存在的内容轻松生成404错误来测试我们的配置。要测试500级错误,我们必须设置一个虚拟代理传递,以便我们可以确保返回正确的页面。
将ProxyPass
指令添加到文件的底部。在本地计算机上发送/proxytest
到端口9000的请求(没有运行服务):
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"
完成后保存并关闭文件。
输入以下命令测试配置文件中的语法错误:
sudo apachectl configtest
解决报告的任何问题。当您的文件不包含语法错误时,请输入以下命令重启Apache:
sudo systemctl restart httpd
现在,当您转到服务器的域或IP地址并请求不存在的文件时,您应该看到我们设置的404页面:
http://server_domain_or_IP/thiswillerror
当您转到我们为虚拟代理通行证设置的位置时,我们将在我们的自定义500级页面上收到“503 service unavailable”错误:
http://server_domain_or_IP/proxytest
您现在可以返回并从Apache配置中删除伪代理传递行。
您现在为您的网站提供了自定义错误页面。即使遇到问题,这是一种简化用户体验个性化的方法。给这些页面一个改进的建议是,设置包含可以获取帮助或更多信息的访问链接。请确保即使发生相关错误也可以访问页面的目标链接。
想要了解更多Linux开源信息教程,请前往腾讯云+社区学习更多知识。
参考文献:《How To Configure Apache to Use Custom Error Pages on CentOS 7》
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有