「Tomcat」アプリケーションに複数のコンテキストパスを設定する
アプリケーションに複数のコンテキストパスを設定したい場合の方法について調べたので、その時のメモ。
( ただ、個人的にどんな状況の場合にこんなことがしたいのか想像もできません・・・ )
ドキュメントを調べてみると、以下に情報があった。
・Apache Tomcat Configuration Reference (6.0.37) - The Context Container
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Defining_a_context
-----
To define multiple contexts that use a single WAR file or directory, use one of the options described in the Naming
section above for creating a Context that has a path that is not related to the base file name.
-----
-----
・Disable autoDeploy and deployOnStartup and define all Contexts in server.xml
・Locate the WAR and/or directory outside of the Host's appBase and use a context.xml file with a docBase attribute to define it.
-----
1. autoDeploy と deployOnStartup を両方無効 ( false ) にして、server.xml に全ての Context を記述する
( server.xml に Context 書くって推奨してなかったはずやのに・・・ )
2. WAR ファイルを appBase の外のディレクトリに配置して、docBase をそのディレクトリに設定する
■ 1 について
server.xml の設定は以下の感じですかね。デプロイするアプリは testApp。
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="false" deployOnStartup="false" xmlValidation="false" xmlNamespaceAware="false"> - 略 - <Context path="/hoge" docBase="testApp"/> <Context path="/uga" docBase="testApp"/> </Host>
webapps 配下に testApp.war を配置して、Tomcat を起動する。
すると http://localhost:8080/hoge、http://localhost:8080/uga でアクセスできるようになった。ただ、webapps 配下を見てみると、hoge、uga ディレクトリが存在している。なんで、Tomcat 的には完全に別アプリケーションとして管理しているってことっぽい。( これでアプリケーションに複数のコンテキストパス持たせたと言えるのか、ちょっと疑問ではあります )
■ 2 について
webapps の外のディレクトリということなので、適当に ${CATALINA_HOME}/webapps2 とか作って testApp.war を配置する。
server.xml を以下の設定に変更する。
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> - 略 - <Context path="/hoge" docBase="/usr/local/tomcat/apache-tomcat-6.0.32/webapps2/testApp"/> <Context path="/uga" docBase="/usr/local/tomcat/apache-tomcat-6.0.32/webapps2/testApp"/> </Host>
Tomcat を起動すると、webapps 配下に hoge、uga ディレクトリが生成されているのが確認できる。 http://localhost:8080/hoge、http://localhost:8080/uga でアクセスできるようになるけど、1 と同様で Tomcat 上では別アプリになる感じでした。
あんまり必要性のない情報やと思いますが、とりあえず、以上です。