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

介绍

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

先决条件

要使用本指南开始,您将需要一个非root用户sudo权限。 您可以与我们一起以下设置此类型的用户为CentOS 7初始设置向导 ,您还需要在系统上已经安装了Apache。 了解如何通过下面的第一步设置这本指南

创建自定义错误页面

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

我们将会把我们的自定义错误页中/var/www/html ,其中的CentOS的Apache安装设置的默认文档根目录。 我们可以给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目录中的Apache配置读取片段。 我们将调用新的文件custom_errors.conf

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

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

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

我们可以使用ErrorDocument指令,每个类型的错误相应的错误页面相关联。 基本上,我们只需要将每个错误的http状态代码映射到我们要在其发生时投放的网页。

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

/etc/httpd/conf.d/custom_errors.conf
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”(指示成功的请求)。

当直接请求错误页时,使用404进行响应

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

/etc/httpd/conf.d/custom_errors.conf
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错误,因为未设置正确的环境变量。

设置500级错误的测试

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

a添加ProxyPass指令到该文件的底部。 发送的请求/proxytest端口9000的本地计算机(其中没有服务正在运行)上:

/etc/httpd/conf.d/custom_errors.conf
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"

保存并在完成后关闭文件。

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

键入以下内容以测试配置文件是否有语法错误:

sudo apachectl configtest

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

sudo systemctl restart httpd

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

http://server_domain_or_IP/thiswillerror

apache自定义404

当您转到我们为虚拟代理传递设置的位置时,我们会在自定义500级页面上收到“503服务无法使用”错误:

http://server_domain_or_IP/proxytest

apache自定义50x

您现在可以返回并从您的Apache配置中删除假代理传递线。

结论

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

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏