仕組み
ServerRoot
Apache がインストールされているディレクトリを設定
Define SRVROOT "c:/Apache24"
ServerRoot "${SRVROOT}"
ServerName
Apache が自分自分自身のホスト名とポート番号を表示するときに使用するホスト名とポート番号を設定
ServerName が指定されていない場合は Web サーバの IP アドレスを逆引きしてホスト名を取得する(hostsファイルやDNS)
Web サーバのホスト名が www.example.com でポート番号が 80 番の場合の例
ServerName www.example.com:80
ServerAdmin
Apache がエラー表示を行う場合などに問い合わせ先となる連絡先メールアドレスまたは参照先 URL を設定
ServerAdmin contact@example.com
Listen
Apache がどのポート番号や IP アドレスのリクエストを受け付けるかを設定
複数のポート番号を受け付ける場合は Listen ディレクティブを複数記述
Listen 80
Listen 192.168.128.1:80
Directory
ディレクトリに対してオプション設定を行う。基本的にDocumentRootと合わせて設定する
<Directory "c:/Apache24/htdocs">
~設定情報を記載する~
</Directory>
Require
ディレクトリのアクセス制御を設定
2.4系以外はアクセス許可の設定の書き方が違う
<Directory "c:/Apache24/htdocs">
Require all granted
Require all denied
Require local
Require ip 192.168.0.1
Require ip 192.168.0
Require not ip 192.168.0.1
Require host example.jp
〇 example.jp
〇 host.example.jp
✕ hostexample.jp
✕ javadrive.jp
Require not host example.jp
</Directory>
Options
Optionsに記載した設定した機能だけが有効になる
<Directory "c:/Apache24/htdocs">
Options オプション1 オプション2 ...
Options All
Options None
Options Indexes
Options FollowSymLinks
Options SymLinksIfOwnerMatch
Options ExecCGI
Options Includes
</Directory>
設定が重複する場合、機能を追加する場合は「+」、削除する場合は「-」を使用する。
<Directory d:/pg/Apache/Apache24/htdocs>
Options Includes ExecCGI
</Directory>
<Directory d:/pg/Apache/Apache24/htdocs/blog>
Options +FollowSymLinks -ExecCGI
</Directory>
AllowOverride
.htaccess で設定を変更できるかどうかの設定
<Directory "c:/Apache24/htdocs">
AllowOverride All
AllowOverride None
AllowOverride AuthConfig Options
</Directory>
ディレクティブの種類
[All]
Files
FilesMatch
[Indexes]
DirectoryIndex
DirectorySlash
[Options]
Options
[FileInfo]
Redirect
AddType
SetEnvIf
ErrorDocument
RewriteEngine
RewriteRule
RewriteCond
RewriteOptions
[AuthConfig]
Require
AuthType
AuthName
AuthBasicProvider
AuthUserFile
AuthGroupFile
AuthDigestDomain
AuthDigestProvider
[Limit]
Order
Allow
Deny
DirectoryIndex
例.http://www.example.com/にアクセスするとDirectoryIndexで指定したファイルを開く
対象のフォルダはDirectoryで指定したフォルダ
※使用しないほうが良い
LoadModule dir_module modules/mod_dir.so
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
指定したファイルが存在しない場合かつ、Options Indexesを指定していると一覧を表示
※Options Indexesの一覧の表示イメージ
Alias
DocumentRoot以外のディレクトリを参照可能にする。
DocumentRootは一つしか設定できないため他に参照したいディレクトリがある場合に使用する。
LoadModule alias_module modules/mod_alias.so
<IfModule alias_module>
Alias /test c:/testDir
<Directory c:/testDir>
Require all granted
</Directory>
</IfModule>
リクエスト:
http://localhost/test/index.html
返すファイル:
c:\testDir\index.html
Redirect
リクエストに含まれるパスが特定のパスと一致した場合、指定した URL へリダイレクトする
LoadModule alias_module modules/mod_alias.so
<IfModule alias_module>
Redirect /data http://www.example.com/newdata
Redirect /data /newdata
</IfModule>
Redirect /data /newdataの場合
リクエスト:
http://(元のホスト名)/data/index.html
リダイレクト先:
http://(元のホスト名)/newdata/index.html
Define
変数を定義する
Define SRVROOT "c:/Apache24"
ServerRoot "${SRVROOT}"
SetEnvIf
リクエストに含まれる情報に基づいて環境変数を設定する
属性値は正規表現を使った値を指定する
環境変数は空白区切りで複数設定することが可能
SetEnvIf 属性 属性値 [!]環境変数[=値]
SetEnvIf Remote_Addr "192\.168\.2\." client
SetEnvIf Remote_Addr "192\.168\.2\.100" !client
SetEnvIf Request_URI "\.(png|jpeg)$" image_file
SetEnvIf Referer "www\.example\.com" my_site
SetEnvIf User-Agent "(iPhone|iPad)" ua=apple
SetEnvIf User-Agent "Android" ua=google