Apache | コンテンツの配置 | MIMEタイプの追加(AddType)
Webサーバーで公開されるファイルは、テキストファイルやHTMLファイルだけではない。画像や動画など、さまざまなファイルを扱うことができる。クライアントから要求されたファイルに対して、WWWサーバーはそのファイルの内容を、要求されたファイルがどの種類のファイルなのかに合わせてクライアントへ返す。それにより、クライアントは受け取ったデータを適切な表示方法で処理する。
基本的なMIMEタイプと拡張子の組み合わせは、標準でmime.typesファイルに登録されている。このファイル名と配置ディレクトリはTypesConfigで設定されている。
httpd.confファイルでTypesConfigを検索すると、次のような内容が見つかる。
<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig conf/mime.types
... 省略 ...
</IfModule>
相対パスで指定されているため、ServerRootからの相対パスになり、実際のファイルは${SRVROOT}\conf\mime.typesになる。
C:\apache\Apache24\conf>dir
C 드라이브의 볼륨에는 이름이 없습니다.
볼륨 일련 번호: XXXX-XXXX
C:\apache\Apache24\conf 디렉터리
2019-11-26 오후 10:39 <DIR> .
2019-11-26 오후 10:39 <DIR> ..
2019-08-09 오후 11:50 1,820 charset.conv
2019-11-26 오후 10:39 <DIR> extra
2019-12-01 오후 11:41 20,686 httpd.conf
2019-08-09 오후 11:50 13,449 magic
2019-08-09 오후 11:50 62,702 mime.types <<<<<<<<<<<<<< 여기 있다.
2019-05-28 오후 10:12 11,259 openssl.cnf
2019-11-26 오후 10:39 <DIR> original
5개 파일 109,916 바이트
4개 디렉터리 456,097,648,640 바이트 남음
C:\apache\Apache24\conf>
mime.typesはテキストファイルなので、テキストエディタで開いて一部を抜粋すると次のようになる。
# MIME type (lowercased) Extensions
# ============================================ ==========
application/json json
text/css css
text/csv csv
text/html html htm
text/plain txt text conf def list log in
MIMEタイプに対応する拡張子が登録されている。たとえば、.htmlおよび.htm拡張子を持つファイルはMIMEタイプがtext/htmlになり、.txtおよび.log拡張子を持つファイルはMIMEタイプがtext/plainになる。
MIMEタイプを追加する
すでに登録されている拡張子以外にも、別の拡張子に対するMIMEタイプを追加登録できる。この場合、mime.typesに追記するのではなく、httpd.confファイルでAddTypeを使って記述することが推奨されている。
AddType MIMEタイプ 拡張子
MIMEタイプと拡張子をセットで指定する。
httpd.confファイルでAddTypeを検索すると、次のような内容が見つかる。
<IfModule mime_module>
... 省略...
#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
... 省略...
</IfModule>
たとえば、PHPファイルである.phpおよび.phpsに対してMIMEタイプを設定するには、次のように記述する。
<IfModule mime_module>
... 省略 ...
#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
... 省略 ...
</IfModule>