apacheのErrorDocumentの動きについて(ローカルパス)

apacheのエラードキュメント(ErrorDocument)の設定を実際に動かしながら検証してみます。

ベース

今回は設定ファイル直下にドキュメントルート(DocumentRoot)とエラードキュメント(ErrorDocument)の設定がある状態のファイルをベースに色々と確認してみたいと思います。

設定

DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 /err.html

結果

$ curl localhost/hoge
<!DOCTYPE html>
<html><body>
<h1>error page</h1>
<p>error page</p>
</body></html>
$

バリエーション

ファイルパスの先頭の「/」なし

エラードキュメントで指定するパスの先頭の「/」を消してみます。

設定

DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 err.html

結果

$ curl localhost/hoge
err.html
$

ファイルパスではなく、「err.html」という文字列として解釈されました。

ファイルパスの先頭の「/」なし(ドキュメントルートの末尾に「/」)

ドキュメントルートの記載で何か変わるか見てみます。

設定

DocumentRoot "/usr/local/apache2/htdocs/"
ErrorDocument 404 err.html

結果

$ curl localhost/hoge
err.html
$

特に変わりなしです。ドキュメントルートの末尾に「/」があろうと無かろうと変わりがないようです。

ファイルパスを「”」で囲む

ファイルパスを「”」で囲ってみます。

設定

DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 "/err.html"

結果

$ curl localhost/hoge
<!DOCTYPE html>
<html><body>
<h1>error page</h1>
<p>error page</p>
</body></html>
$

「”」で囲ったとしても、きちんとファイルパスとして解釈してくれるようです。

ファイルパスにクエリストリングやアンカー付き

ファイルパスにクエリストリング(?=xxxx)やアンカー(#xxxx)があった場合に動きが変わるのかを見てみます。

設定

DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 /err.html?q=a&p=b#top

結果

$ curl localhost/hoge/
<!DOCTYPE html>
<html><body>
<h1>error page</h1>
<p>error page</p>
</body></html>
$

特に変わりなく(エラーとならず)対象のHTMLファイルが表示されました。

ヘッダ情報なども確認しましたが、特に変な情報が付与されているということも無いようです。

$ curl localhost/hoge/ -v
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> GET /hoge/ HTTP/1.1
> Host: localhost
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Wed, 26 Feb 2020 14:24:22 GMT
< Server: Apache/2.4.41 (Unix)
< Last-Modified: Wed, 26 Feb 2020 13:35:16 GMT
< ETag: "52-59f7aacb62d00"
< Accept-Ranges: bytes
< Content-Length: 82
< Content-Type: text/html
<
<!DOCTYPE html>
<html><body>
<h1>error page</h1>
<p>error page</p>
</body></html>
* Connection #0 to host localhost left intact
$

同じエラードキュメントの設定が複数

同じエラードキュメントの設定がある場合にどうなるかを見てみます。

設定

DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 /err.html
ErrorDocument 404 /err.html

結果

これはそもそも起動できるのか否かから見てみます。

root@8dfee30c9824:/usr/local/apache2# apachectl restart
root@8dfee30c9824:/usr/local/apache2#

大丈夫なようです。以下の通りアクセス結果もOKです。

$ curl localhost/hoge/
<!DOCTYPE html>
<html><body>
<h1>error page</h1>
<p>error page</p>
</body></html>
$

異なるエラードキュメント設定あり

複数箇所でエラードキュメントの設定があり、その内容が異なる場合にどうなるかを見てみます。

設定

以下のように設定してみます。

DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 /err.html
ErrorDocument 404 /err_hoge.html

結果

以下のようになりました。

$ curl localhost/hoge/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
$

なお「err_hoge.html」は用意していないのでデフォルトのエラーが表示されています。

設定(逆バージョン)

先ほどとは順序を逆にしてみます。

DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 /err_hoge.html
ErrorDocument 404 /err.html

結果(逆バージョン)

$ curl localhost/hoge/
<!DOCTYPE html>
<html><body>
<h1>error page</h1>
<p>error page</p>
</body></html>
$

今度は検証用に用意していた「err.html」が表示されました。

まとめ

エラードキュメントの設定は後勝ちになっているようです。

ドキュメントルート複数

ドキュメントルートの設定が複数ある場合にどうなるかを見てみます。

設定

以下のように設定してみます。

DocumentRoot "/usr/local/apache2/htdocs"
DocumentRoot "/tmp/"
ErrorDocument 404 /err.html

結果

以下のような結果になりました。

$ curl localhost/hoge/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
$

デフォルトのメッセージになったので「/tmp/」(ファイルを配置していない方)が優先されていることが分かります。

設定(逆バージョン)

次に先ほどとは順序を逆にしてみます。

DocumentRoot "/tmp/"
DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 /err.html

結果(逆バージョン)

$ curl localhost/hoge/
<!DOCTYPE html>
<html><body>
<h1>error page</h1>
<p>error page</p>
</body></html>
$

今度は用意していたHTMLファイルが表示されました。「/usr/local/apache2/htdocs」が優先されていることが分かります。

まとめ

ドキュメントルートの設定は後勝ちになっているようです。

ドキュメントルート複数(間にエラードキュメント)

先ほどドキュメントルートは後勝ちになっていることが分かりましたが、エラードキュメントの記載が途中にあっても同じ結果になるか見てみます。

設定

DocumentRoot "/usr/local/apache2/htdocs"
ErrorDocument 404 /err.html
DocumentRoot "/tmp/"

結果

$ curl localhost/hoge/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
$

「/tmp/」の方が優先されました。特に「ErrorDocument」が登場する前の分だけが有効ということも無いようです。

その他

今回はファイル直下に設定がある状態をベースに色々と試しましたが、他にもVirtualHostを使った状態をベースに色々と試しています。そちらは以下にまとめています。

コメント

タイトルとURLをコピーしました