「リソースに任意の HTTP ヘッダを加える」機能を持つモジュール mod_headers は Apache 1.2 以降に搭載されている。
mod_headers をインストールすることで、HTTPレスポンスヘッダを制御する Header ディレクティブを使うことができる。

Apache 1.3.37 と mod_headers をインストール


# cd /usr/local/src/apache_1.3.37/src/modules/standard
# /usr/local/apache/bin/apxs -c mod_headers.c
gcc -DLINUX=22 -DHAVE_SET_DUMPABLE -DUSE_HSREGEX -DUSE_EXPAT -I../lib/expat-lite -fpic -DSHARED_MODULE -I/usr/local/apache/include  -c mod_headers.c
gcc -shared -o mod_headers.so mod_headers.o
# /usr/local/apache/bin/apxs -i mod_headers.so
cp mod_headers.so /usr/local/apache/libexec/mod_headers.so
chmod 755 /usr/local/apache/libexec/mod_headers.so

Header ディレクティブのドキュメント

ドキュメントを見る。

The action it performs is determined by the first argument. This can be one of the following values:

* set
  The response header is set, replacing any previous header with this name
* append
  The response header is appended to any existing header of the same name. When a new value is merged onto an existing header it is separated from the existing header with a comma. This is the HTTP standard way of giving a header multiple values.
* add
  The response header is added to the existing set of headers, even if this header already exists. This can result in two (or more) headers having the same name. This can lead to unforeseen consequences, and in general "append" should be used instead.
* unset
  The response header of this name is removed, if it exists. If there are multiple headers of the same name, all will be removed.
(中略)
The Header directives are processed just before the response is sent by its handler. These means that some headers that are added just before the response is sent cannot be unset or overridden. This includes headers such as "Date" and "Server".

Apache module mod_headers

ざっくり翻訳。

* set : レスポンスヘッダを追加する。同じ名前のヘッダがあれば置き換える。
* append : すでに存在しているレスポンスヘッダに内容を追加する。
* add : レスポンスヘッダを追加する。同じ名前のヘッダがあっても、別のヘッダとして追加する(複数の同名ヘッダが存在することになる)。
* unset : レスポンスヘッダを削除する。同じ名前のヘッダが複数ある場合は、それらのヘッダをすべて削除する。
(中略)
Header ディレククティブは、ハンドラによって送られる前に処理される。レスポンスが送られる前のヘッダは削除したり置き換えたりできない。Date や Server ヘッダとか。

http.conf の設定

WebBrowser -> Apache -> Tomcat なリバースプロキシで出力される Server と X-Cache ヘッダを置き換え&削除しようと思って設定する。

httpd.conf の一部


LoadModule proxy_module libexec/mod_proxy.so
ProxyRequests Off
ProxyPass /rp/ http://localhost:8180/ws/
ProxyPassReverse /rp/ http://localhost:8180/ws/
 
LoadModule headers_module libexec/mod_headers.so
Header set Server "Pikachu"
Header set Zura "yes"
Header unset X-Cache

結果

レスポンスヘッダの内容(一部)


HTTP/1.x 200 OK
Server: Apache-Coyote/1.1
Zura: yes
X-Cache: MISS from localhost.localdomain

まぁ、やっぱり Server ヘッダは置き換えられなかった。
Zura ヘッダはちゃんと追加された。
X-Cache ヘッダも削除できず。残念。

Apache 2.2 の mod_headers ではもっと柔軟な制御ができるっぽい。

参考

tags: zlashdot Apache Apache Tomcat

Posted by NI-Lab. (@nilab)