我已经创建了许多自定义错误页面,并将其放在全局配置中,因此它将应用于所有虚拟主机:
Alias /common /var/www/common
ErrorDocument 400 /common/400.shtml
ErrorDocument 401 /common/401.shtml
ErrorDocument 403 /common/403.shtml
ErrorDocument 404 /common/404.shtml
ErrorDocument 405 /common/405.shtml
ErrorDocument 408 /common/408.shtml
ErrorDocument 410 /common/410.shtml
ErrorDocument 421 /common/421.shtml
ErrorDocument 500 /common/500.shtml
ErrorDocument 501 /common/501.shtml
其中一些比其他的更容易测试。
对于其中一些,我必须使用 mod_rewrite ( RewriteRule ^(.*)$ - [L,R=XXX]
) 强制错误代码,并且它们都按预期工作
但是,我希望尽可能“有机地”测试其中尽可能多的错误,即“真实地”引发错误,而不是使用 mod_rewrite 强制引发错误。其中一些错误(如 401/403/404)可在浏览器中轻松测试,而其他错误(如 405 和 501)则需要 Curl,还有一些错误需要手动 telnet。
我遇到的问题是,当我自然引发 400 或 408 时,Apache 似乎会忽略我的 ErrorDoc 并提供默认错误页面:
400 测试(发送没有 Host 标头的 HTTP/1.1 请求):
# telnet XXXXX
Trying XXXXX
Connected to XXXXX
Escape character is '^]'.
GET / HTTP/1.1
HTTP/1.1 400 Bad Request
Date: Mon, 03 May 2021 16:35:14 GMT
Server: Apache
Content-Length: 546
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<p>Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache Server at XXXXXX</address>
</body></html>
Connection closed by foreign host.
408 测试(不发送空行来结束请求,而是等待服务器超时):
# telnet XXXXX
Trying XXXXX...
Connected XXXX
Escape character is '^]'.
GET / HTTP/1.0
HTTP/1.1 408 Request Timeout
Date: Mon, 03 May 2021 16:27:35 GMT
Server: Apache
Content-Length: 420
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>408 Request Timeout</title>
</head><body>
<h1>Request Timeout</h1>
<p>Server timeout waiting for the HTTP request from the client.</p>
<hr>
<address>Apache Server at XXXXXX</address>
</body></html>
Connection closed by foreign host.
#
有趣的是,服务器对这些说“charset = iso-8859-1” - 我习惯看到“charset = utf-8”,因为我在全局配置中有“AddDefaultCharset utf-8”并且我的所有文件都是UTF-8。
阿帕奇/2.4.41