1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "./style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="./style/manual.ja.xsl"?>
4 <!-- English Revision: 659902:1725855 (outdated) -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="env.xml.meta">
25 <title>Apache の環境変数</title>
28 <p>Apache HTTP サーバは<em>環境変数</em>と呼ばれる、名前のついた
29 変数に情報を記憶する仕組みを提供しています。この情報はログ収集や
30 アクセス制御などのいろいろな操作を制御するために使うことができます。
31 これらの変数は CGI スクリプトなどの外部プログラムと通信するためにも
32 使われます。この文書はそれらの変数の操作方法と使用方法をいくつか
35 <p>これらの変数は<em>環境変数</em>と呼ばれていますが、オペレーティング
36 システムによって制御されている環境変数と同じではありません。
37 実際は、これらの変数は Apache の内部構造の中に記憶され、操作されています。
38 それらは、CGI や SSI スクリプトに渡されたときだけ、実際の
39 オペレーティングシステムの環境変数になります。サーバ自身が
40 実行されているオペレーティングシステムの環境を操作したい場合は、
41 オペレーティングシステムのシェルが提供している標準の環境変数の
45 <section id="setting">
46 <title>環境変数の設定</title>
49 <module>mod_env</module>
50 <module>mod_rewrite</module>
51 <module>mod_setenvif</module>
52 <module>mod_unique_id</module>
55 <directive module="mod_setenvif">BrowserMatch</directive>
56 <directive module="mod_setenvif">BrowserMatchNoCase</directive>
57 <directive module="mod_env">PassEnv</directive>
58 <directive module="mod_rewrite">RewriteRule</directive>
59 <directive module="mod_env">SetEnv</directive>
60 <directive module="mod_setenvif">SetEnvIf</directive>
61 <directive module="mod_setenvif">SetEnvIfNoCase</directive>
62 <directive module="mod_env">UnsetEnv</directive>
66 <section id="basic-manipulation">
67 <title>基本的な環境の操作</title>
69 <p>Apache において環境変数を設定する一番基本的な方法は、
70 無条件に環境変数を設定する <directive module="mod_env"
71 >SetEnv</directive> ディレクティブを使用することです。
72 <directive module="mod_env">PassEnv</directive>
73 ディレクティブにより、Apache が起動されたシェルの
77 <section id="conditional">
78 <title>リクエスト毎に条件に基づいて設定する</title>
80 <p>より柔軟性を高めるために、mod_setenvif
81 で提供されているディレクティブを使用することで、リクエストの
82 特性に基づいて環境変数を設定することができます。例えば、特定のブラウザ
83 (User-Agent) のリクエストや特定の Referer [意図的な綴りです]
84 <transnote>正しい綴りは referrer ですが、HTTP の仕様では Referer
85 となっています</transnote>ヘッダが見つかったときのみ変数を設定することができます。
86 mod_rewrite の <directive module="mod_rewrite">RewriteRule</directive>
87 ディレクティブにおいて環境変数を設定する <code>[E=...]</code>
89 より柔軟な設定を行なうことができます。</p>
92 <section id="unique-identifiers">
95 <p>mod_unique_id は、非常に限られた条件の下で
96 「すべて」のリクエストについて、一意であることが保証されている値を環境変数
97 <code>UNIQUE_ID</code> に設定します。</p>
100 <section id="standard-cgi">
101 <title>標準 CGI 変数</title>
103 <p>Apache の設定ファイルで設定された環境変数とシェルから渡される
104 環境変数に加えて、CGI スクリプトと SSI ページには <a
105 href="http://cgi-spec.golux.com">CGI の仕様</a>で要求されている、
106 リクエストのメタ情報を持った環境変数の組が提供されます。</p>
109 <section id="caveats">
110 <title>いくつかの注意</title>
113 <li>環境を操作するディレクティブを使って標準 CGI
114 変数を上書きしたり変更したりすることはできません。</li>
116 <li>CGI スクリプトを起動するために <program>suexec</program>
117 が使用されている場合、CGI スクリプトが起動するために、環境変数は<em
118 >安全</em>な環境変数の組に整理されます。
119 この安全な環境変数の集合は、コンパイル時に <code>suexec.c</code>
122 <li>移植性のために、環境変数の名前はアルファベット、
123 数字とアンダースコア <transnote>'_'</transnote> だけから成ります。
124 さらに、最初の文字は数字であってはいけません。
125 この制限に合わない文字は CGI スクリプトと SSI
126 ページに渡されるときにアンダースコアに置換されます。</li>
128 <li><directive module="mod_env">SetEnv</directive> はリクエスト処理の
130 <directive module="mod_setenvif">SetEnvIf</directive> や
131 <directive module="mod_rewrite">RewriteCond</directive>
132 などからは、変数がそこで設定されていることがわかりません。</li>
137 <title>環境変数の使用</title>
141 <module>mod_authz_host</module>
142 <module>mod_cgi</module>
143 <module>mod_ext_filter</module>
144 <module>mod_headers</module>
145 <module>mod_include</module>
146 <module>mod_log_config</module>
147 <module>mod_rewrite</module>
150 <directive module="mod_authz_host">Allow</directive>
151 <directive module="mod_log_config">CustomLog</directive>
152 <directive module="mod_authz_host">Deny</directive>
153 <directive module="mod_ext_filter">ExtFilterDefine</directive>
154 <directive module="mod_headers">Header</directive>
155 <directive module="mod_log_config">LogFormat</directive>
156 <directive module="mod_rewrite">RewriteCond</directive>
157 <directive module="mod_rewrite">RewriteRule</directive>
161 <section id="cgi-scripts">
162 <title>CGI スクリプト</title>
164 <p>環境変数の主な利用法の一つは、CGI スクリプトに情報を伝えることです。
165 上で説明されているように、CGI スクリプトに渡される環境変数は Apache
166 の設定により設定される変数に加えて、リクエストの標準のメタ情報を含んでいます。
167 詳細は <a href="howto/cgi.html">CGI チュートリアル</a>
171 <section id="ssi-pages">
172 <title>SSI ページ</title>
174 <p>mod_include の <code>INCLUDES</code> フィルタで処理される
175 server-parsed (SSI) ドキュメントでは、<code>echo</code>
177 また、ページのある部分がリクエストの性質に応じて変更されるように、
178 環境変数をフロー制御要素で使うことができます。詳細は
179 <a href="howto/ssi.html">SSI チュートリアル</a> を参照してください。</p>
182 <section id="access-control">
183 <title>アクセス制御</title>
185 <p><code>allow from env=</code> ディレクティブと <code>deny from env=</code>
186 ディレクティブを使用して、サーバへのアクセスを環境変数の値で制御することができます。
187 <directive module="mod_setenvif">SetEnvIf</directive>
188 ディレクティブと組み合わせることで、クライアントの特性に基づいて
189 サーバへのアクセス制御を柔軟に行なうことができるようになります。
190 たとえば、これらのディレクティブを使用して、特定のブラウザ (User-Agent)
191 からのアクセスを拒否することができます。</p>
194 <section id="logging">
195 <title>条件付きログ記録</title>
197 <p><directive module="mod_log_config">LogFormat</directive>
198 ディレクティブのオプション <code>%e</code>
199 を使用することで、環境変数をアクセスログに記録することができます。さらに、
200 <directive module="mod_log_config">CustomLog</directive>
201 ディレクティブの条件分岐式を使用することで、
202 環境変数の値によってリクエストをログに記録するかどうかを決めることができます。
203 <directive module="mod_setenvif">SetEnvIf</directive>
205 どのリクエストをログに記録するかを柔軟に制御することが可能になります。たとえば、
206 <code>gif</code> で終わるファイル名へのリクエストはログに記録しない、
207 違うサブネットのクライアントからのリクエストだけをログに記録する、
211 <section id="response-headers">
212 <title>条件付き応答ヘッダ</title>
214 <p><directive module="mod_headers">Header</directive>
215 ディレクティブは環境変数の存在や不在によってクライアントへの応答に特定の
216 HTTP ヘッダを付けるかどうかを決めることができます。
217 これにより、たとえば、クライアントからのリクエスト
218 にあるヘッダがある場合にのみ特定の応答ヘッダを送る、というようなことが
223 <section id="external-filter">
224 <title>外部フィルタの適用</title>
226 <p><directive module="mod_ext_filter">ExtFilterDefine</directive>
228 <module>mod_ext_filter</module> で設定される外部フィルタは、
229 <code>disableenv=</code> と <code>enableenv=</code>
230 オプションを使って、環境変数による条件付き適用ができます。</p>
233 <section id="url-rewriting">
234 <title>URL の書き換え</title>
236 <p><directive module="mod_rewrite">RewriteCond</directive>
237 ディレクティブで<em>評価文字列</em>として
238 <code>%{ENV:...}</code> 式を指定することで、mod_rewrite
239 の書き換えエンジンが環境変数に基いて条件分岐を行なうことができます。
240 mod_rewrite が使用可能な変数で <code>ENV:</code> が前についていない変数は、
241 実際は環境変数ではないということに注意してください。
242 それらは他のモジュールからは使用できない mod_rewrite 用の特別な変数です。
247 <section id="special">
248 <title>特別な目的の環境変数</title>
250 <p>互換性の問題を解決するために、特定のクライアントと通信しているときは
251 Apache の動作を変更できる機構が導入されました。できるだけ柔軟にするために、
252 これらの機構は環境変数を定義することで呼び出されます。普通は、
253 <directive module="mod_setenvif">BrowserMatch</directive>
254 ディレクティブを使いますが、たとえば <directive module="mod_env"
255 >SetEnv</directive> ディレクティブや <directive module="mod_env"
256 >PassEnv</directive> ディレクティブも使用することができます。</p>
258 <section id="downgrade">
259 <title>downgrade-1.0</title>
261 <p>これを指定することで、リクエストが HTTP/1.0
262 より新しいプロトコルの場合でも、HTTP/1.0 として扱われます。</p>
265 <section id="force-gzip">
266 <title>force-gzip</title>
267 <p><code>DEFLATE</code> フィルタが使用するように設定されているときに、
268 この環境変数はブラウザの accept-encoding の設定を無視して常に
269 圧縮された出力を送るようにします。</p>
271 <section id="force-no-vary">
272 <title>force-no-vary</title>
274 <p>応答ヘッダがクライアントに送られる前に <code>Vary</code>
276 クライアントの中にはこのフィールドを正しく解釈しないものがあります。
277 この変数を設定することでその問題を回避することができます。
278 この変数を設定すると、<strong>force-response-1.0</strong>
282 <section id="force-response">
283 <title>force-response-1.0</title>
285 <p>これが設定されていると、HTTP/1.0 リクエストを発行するクライアントに対しては
286 常に HTTP/1.0 で応答するようになります。この機能は、
287 元々は AOL のプロキシの問題のために実装されました。HTTP/1.0 クライアントの中には、
288 HTTP/1.1 の応答を返されると正しく動作しないものがあるかもしれません。
289 この機能を使用することで、そのようなクライアントとの間の互換性問題を解決できます。</p>
292 <section id="gzip-only-text-html">
293 <title>gzip-only-text/html</title>
295 <p>これが 1 に設定されると、この変数は <code>text/html</code>
296 以外のコンテントタイプに対する、<module>mod_deflate</module>
297 提供の <code>DEFLATE</code> 出力フィルタを無効にします。
298 また、静的に、既に圧縮されたファイルを使用したい場合、
299 (gzip だけでなく、"identity" と異なる全てのエンコードに対して)
300 <module>mod_negotiation</module> も変数を評価します。</p>
303 <section id="no-gzip"><title>no-gzip</title>
304 <p>セットされると、<module>mod_deflate</module> の
305 <code>DEFLATE</code> フィルタがオフになります。
306 そして <module>mod_negotiation</module>
307 はエンコードされたリソースを送らないようにします。</p>
310 <section id="nokeepalive">
311 <title>nokeepalive</title>
313 <p>これが設定されている場合は、<directive module="core"
314 >KeepAlive</directive> を使用しないようにします。</p>
316 <section id="prefer-language"><title>prefer-language</title>
318 <p><module>mod_negotiation</module> の挙動に影響を与えます。
319 (<code>en</code>, <code>ja</code>, <code>x-klingon</code>といった)
320 言語タグが格納されていれば、その言語の variant を送信しようとします。
321 そのような variant がない場合は、
322 通常の<a href="content-negotiation.html">ネゴシエーション</a>処理が
328 <section id="redirect-carefully">
329 <title>redirect-carefully</title>
331 <p>これはクライアントへのリダイレクトの送信をサーバがより注意深く
333 これは通常、リダイレクトに際してクライアントに
334 問題があることが分かっている場合に使われます。この機能は元々は
335 マイクロソフトのウェブフォルダのソフトが DAV
336 メソッドによるディレクトリのリソースへのリダイレクトの扱いに
337 問題がり、それを回避するために実装されました。</p>
341 <section id="suppress-error-charset">
342 <title>suppress-error-charset</title>
344 <p><em>Apache 2.2 以降で利用可能</em></p>
346 <p>クライアントのリクエストに対する応答としてリダイレクトを送信する際、
347 レスポンスにはリダイレクトが自動的に行なえない (行なわれない)
349 通常、このテキストに合致したキャラクタセット、ISO-8859-1
351 <p>しかし、リダイレクト先が別の文字セットを使っている場合、
353 リダイレクト先の実際の文字セットの代わりに、
354 リダイレクト元の文字セットを使ってしまうことがあります。
355 その結果、例えば変な描画が行なわれたりして、読めなくなったりします。</p>
356 <p>この環境変数を設定することで、リダイレクションテキストに対する
357 キャラクタセットの指定を除去しますので、それら問題のあるブラウザでも
358 リダイレクト先の文字セットを正しく使うようにできます。</p>
360 <note type="warning">
361 <title>セキュリティ</title>
363 <p>文字セットを指定せずにエラーページを送信すると、
364 クロスサイトスクリプティング <transnote>XSS</transnote>
366 HTTP/1.1 仕様に準拠していなくて、コンテンツの中身から文字セットを
367 "推測" しようとするブラウザ (MSIE) が実際にあるからです。
368 そのようなブラウザは UTF-7 文字セットを使って簡単に騙すことができます。
369 クロスサイトスクリプティング攻撃を防ぐために実装されている
370 通常のエスケープ機構が、入力データ中にある UTF-7 で
371 エンコードされたコンテンツ (リクエスト URI など) には
377 <section id="proxy"><title>force-proxy-request-1.0, proxy-nokeepalive, proxy-sendchunked, proxy-sendcl</title>
379 <p>これらの指示子は <module>mod_proxy</module> の挙動を変更します。
380 詳細は <module>mod_proxy</module> のドキュメントをご参照ください。</p>
385 <section id="examples">
388 <section id="misbehaving">
389 <title>おかしな挙動をするクライアントに対してプロトコルの動作を変更する</title>
391 <p>クライアントに関する既知の問題に対処するために、以下の行を
392 httpd.conf に入れることを推奨しています。</p>
393 <p>古いバージョンの Apache では、クライアントの問題に対応するために
394 httpd.conf に次の行を加えるよう推奨されていましたが、
395 今となっては、問題としていたクライアントは実際には見かけることは
396 なくなってきたので、この設定はもはや必要ないかもしれません。</p>
399 # The following directives modify normal HTTP response behavior.
400 # The first directive disables keepalive for Netscape 2.x and browsers that
401 # spoof it. There are known problems with these browser implementations.
402 # The second directive is for Microsoft Internet Explorer 4.0b2
403 # which has a broken HTTP/1.1 implementation and does not properly
404 # support keepalive when it is used on 301 or 302 (redirect) responses.
406 BrowserMatch "Mozilla/2" nokeepalive
407 BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
410 # The following directive disables HTTP/1.1 responses to browsers which
411 # are in violation of the HTTP/1.0 spec by not being able to grok a
412 # basic 1.1 response.
414 BrowserMatch "RealPlayer 4\.0" force-response-1.0
415 BrowserMatch "Java/1\.0" force-response-1.0
416 BrowserMatch "JDK/1\.0" force-response-1.0</pre></example>
419 <section id="no-img-log">
420 <title>画像へのリクエストをアクセスログに記録しない</title>
422 <p>この例では、画像へのリクエストがアクセスログに現れないようにします。
423 これを変更することで、特定のディレクトリのログ収集をやめたり、
424 特定のホストからのリクエストのログ収集をやめたりすることが簡単にできます。
427 SetEnvIf Request_URI \.gif image-request
428 SetEnvIf Request_URI \.jpg image-request
429 SetEnvIf Request_URI \.png image-request
430 CustomLog logs/access_log common env=!image-request</pre></example>
433 <section id="image-theft">
434 <title>「画像の盗用」を防ぐ</title>
436 <p>この例は、別のサーバにいる人が、あなたのサーバにある画像を
437 inline 画像として使用することを防ぎます。
438 これは推奨されている設定ではありませんが、ある限定された状況では有効です。
439 ここでは、すべての画像は <code>/web/images</code>
440 というディレクトリにあると仮定します。</p>
442 SetEnvIf Referer "^http://www\.example\.com/" local_referal
443 # Allow browsers that do not send Referer info
444 SetEnvIf Referer "^$" local_referal
445 <Directory /web/images>
448 Allow from env=local_referal
449 </Directory></pre></example>
451 <p>この手法に関する詳しい情報は ServerWatch にあるチュートリアル
452 「<a href="http://www.serverwatch.com/tutorials/article.php/1132731"
453 >Keeping Your Images from Adorning Other Sites</a>