Apache 連携 [mod_proxy_ajp]

Tomcat 4 や 5 の時代は、mod_jk2 への取り組みが結果的に失敗し、開発のメインが mod_jk に戻るなど Apache 連携が混沌としていた時代もありましたが、 これらの失敗から Apache 2.2 以降では Apache と Tomcat の連携を行うモジュールが mod_proxy_ajp として最初から提供され、 設定方法も非常にシンプルかつ柔軟になりました。
尚、mod_proxy_ajp モジュールを使用する場合は、mod_proxy モジュールも必要となります。

mod_proxy_ajp での Apache 連携は非常に簡単で、Apache の設定ファイルへ Tomcat 側 Web アプリケーションのマッピングを記述するだけです。 実は、Fedora の Apache 2.2 ではデフォルトで Apache 連携が有効になっているので、 ここでは例として、Tomcat の各アプリケーションに対して LAN 側からのみアクセス可能としてみたいと思います。

Apache 設定

1. Apache Proxy AJP 設定ファイルを編集します。

# vi /etc/httpd/conf.d/proxy_ajp.conf

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

#
# When loaded, the mod_proxy_ajp module adds support for
# proxying to an AJP/1.3 backend server (such as Tomcat).
# To proxy to an AJP backend, use the "ajp://" URI scheme;
# Tomcat is configured to listen on port 8009 for AJP requests
# by default.
#

# 
# Uncomment the following lines to serve the ROOT webapp
# under the /tomcat/ location, and the jsp-examples webapp
# under the /examples/ location.
#
デフォルトの設定をコメント化します
#ProxyPass /tomcat/ ajp://localhost:8009/
#ProxyPass /examples/ ajp://localhost:8009/jsp-examples/

Location を使用して設定してみます
<Location /tomcat>
  ProxyPass ajp://localhost:8009/
  Order allow,deny
  Allow from 192.168.11
</Location>
<Location /examples>
  ProxyPass ajp://localhost:8009/examples
  Order allow,deny
  Allow from 192.168.11
</Location>
<Location /manager>
  ProxyPass ajp://localhost:8009/manager
  Order allow,deny
  Allow from 192.168.11
</Location>
<Location /host-manager>
  ProxyPass ajp://localhost:8009/host-manager
  Order allow,deny
  Allow from 192.168.11
</Location>
<Location /probe>
  ProxyPass ajp://localhost:8009/probe
  Order allow,deny
  Allow from 192.168.11
</Location>

  • Location 内で認証関係の設定を行い Apache 側の認証機能を使用することも可能です。
  • Apache 側で圧縮通信を有効にしている場合、きちんと適用されます。

2. Apache の設定変更に間違いがないか検証します。

# apachectl configtest
Syntax OK

3. Apache を再起動します。

# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

  • mod_jk 使用時の連携とは違い、mod_proxy_ajp では Apache と Tomcat の起動順は特に関係ありません。

Tomcat 設定

Apache と連携させるだけであれば Tomcat の設定変更は特に必要ありませんが、 本サイトでは 1 歩踏み込んで Apache 連携時に Tomcat で使用される AJP コネクタについて実践的な設定変更をしてみたいと思います。

1. Tomcat サーバー設定ファイルを編集します。

# vi /usr/local/tomcat/conf/server.xml
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">

(中略)

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container", 
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    Apache と連携することによって HTTP コネクタが不要になるのでコメント化します。※省略可
<-- 
    <Connector port="8080" protocol="HTTP/1.1" 
               maxThreads="150" connectionTimeout="20000" 
               redirectPort="8443" />
 -->

    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    AJP コネクタの設定を変更します。※省略可
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"
      ホスト名参照を行わないようにします。
      enableLookups="false"
      ローカルアドレスからのみ受け付けるようにします。
      address="127.0.0.1"
      必要により最小スレッド数や最大スレッド数などを設定します。
      ※設定する値は一般的に Web サーバー側の値より小さくてかまいません。
      minSpareThreads="4" maxSpareThreads="50" maxThreads="200" backlog="10"
      接続タイムアウト(ミリ秒)を指定します。
      connectionTimeout="20000"
      最大 POST サイズなどを指定します。
      maxPostSize="2097152" maxSavePostSize="4096"
      必要により URI エンコーディングなどを指定します。
      URIEncoding="Windows-31J" useBodyEncodingForURI="false"
      xpoweredBy="false" />

(以下略)

2. Tomcat を再起動します。

# service tomcat stop
# service tomcat start

Google