【Apache】httpd.conf

ファイルの場所

C:\Apache24\conf

仕組み

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

#指定したIPのみのリクエストを受け付ける
Listen 192.168.128.1:80

DocumentRoot

Apache が Web サーバとして外部に公開するコンテンツを配置するディレクトリを設定
例.DocumentRootのディレクトリにsample.htmlを配置した場合
http://www.example.com/index.htmlで開くことができる

DocumentRoot "c:/Apache24/htdocs"

Directory

ディレクトリに対してオプション設定を行う。基本的にDocumentRootと合わせて設定する

<Directory "c:/Apache24/htdocs">
 ~設定情報を記載する~
</Directory>
Require

ディレクトリのアクセス制御を設定
2.4系以外はアクセス許可の設定の書き方が違う

<Directory "c:/Apache24/htdocs">
    #すべてのアクセスを許可
    Require all granted

    #すべてのアクセスを拒否
    Require all denied

    #ローカルホストからのアクセスを許可
    Require local

    #指定のIPアドレスのみ許可
    Require ip 192.168.0.1
    #IPアドレスの一部でも可能
    Require ip 192.168.0

    #指定のIPアドレスを拒否
    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

    #DirectoryIndex ディレクティブで指定したファイルが存在しない場合、一覧を表示
    Options Indexes

    #対象のディレクトリ内でシンボリックリンク(ショートカットみたいなもの)の実行を許可する
    Options FollowSymLinks

    #シンボリックリンクの所有ユーザ ID と同じ場合にのみシンボリックリンクの実行を許可する
    Options SymLinksIfOwnerMatch

    #対象のディレクトリ内で CGI のプログラムを実行を許可する
    Options ExecCGI

    #対象のディレクトリ内でSSIが有効になる
    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

    #.htaccessの利用を許可しない
    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>
    #この場合、http://www.example.com/でhttp://www.example.com/index.htmlを開く
    DirectoryIndex index.html
</IfModule>

指定したファイルが存在しない場合かつ、Options Indexesを指定していると一覧を表示
※Options Indexesの一覧の表示イメージ

Alias

DocumentRoot以外のディレクトリを参照可能にする。
DocumentRootは一つしか設定できないため他に参照したいディレクトリがある場合に使用する。

#下記のモジュールを使うためコメントアウトになっていないことを確認する
LoadModule alias_module modules/mod_alias.so

<IfModule alias_module>
    #Alias URLパス ファイルパス
    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>
    #先頭から /data と一致した場合に http://www.example.com/dataへリダイレクト
    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

リクエストに含まれる情報に基づいて環境変数を設定する
属性値は正規表現を使った値を指定する
環境変数は空白区切りで複数設定することが可能

Remote_Host クライアントのホスト名
Remote_Addr クライアントのIPアドレス
Server_Addr サーバのIPアドレス
Request_Method HTTP リクエストメソッド(GET、POSTなど)
Request_Protocol プロトコル名(HTTP/1.1 など)
Request_URI リクエストのURI
SetEnvIf 属性 属性値 [!]環境変数[=値]
# IP アドレスが192.168.2 で始まるアドレスだった場合は環境変数 client を定義
SetEnvIf Remote_Addr "192\.168\.2\." client
# IP アドレスが192.168.2.100 だった場合だけ環境変数 client の定義を解除
SetEnvIf Remote_Addr "192\.168\.2\.100" !client

# リクエスト URI の末尾が .png または .jpeg だった場合に環境変数を定義
SetEnvIf Request_URI "\.(png|jpeg)$" image_file

# リファラーに www.example.com が含まれている場合に環境変数を定義
SetEnvIf Referer "www\.example\.com" my_site

# ユーザーエージェントに iPhone や iPad または Android が含まれている場合に環境変数を定義
SetEnvIf User-Agent "(iPhone|iPad)" ua=apple
SetEnvIf User-Agent "Android" ua=google