LoginSignup
2
0

More than 1 year has passed since last update.

CentOS8.3でhttpdとTomcat9を連携させる手順(複数のProxyPass)

Last updated at Posted at 2020-12-15
../

CentOS8.2でApache httpd2.4とTomcat9をmod_proxy_ajpを利用して連携する際に、複数のProxyPassを記述したい。その手順をメモしておく。

Apache httpd2.4とTomcat9連携の初期状態

Tomcat9のデフォルトのインストールの後、kankeriアプリとxxxアプリを追加したとすると、webappsの配下は以下のようになっている。${CATALINA_HOME}は、/opt/tomcat9/ とする。

/opt/tomcat9/webapps/
   +-- ROOT/
   +-- docs/
   +-- examples/
   +-- manager/
   +-- host-manager/
   +-- kankeri/
   +-- xxx/

また、デフォルトのインストールの後に、8080ポートで以下のアクセスは可能になっている。(※アクセス可能なクライアントPCは、IPアドレスで制限される。)

http://kankeri.com:8080/
http://kankeri.com:8080/docs
http://kankeri.com:8080/examples
http://kankeri.com:8080/manager
http://kankeri.com:8080/host-manager

さらに、以下のようにバーチャルホストを定義して、http://kankeri.com/ でkankeriアプリにアクセス可能になっていると仮定する。httpdとTomcat9を連携させる手順を参照のこと。

$ vi /etc/httpd/conf.d/vhost-02-kankeri-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost kankeri.com:443>
ServerName kankeri.com
ServerAlias www.kankeri.com
ServerAdmin webmaster@kankeri.com
DocumentRoot "/opt/tomcat9/webapps/kankeri"

<Directory "/opt/tomcat9/webapps/kankeri">
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

ProxyPass  /  ajp://localhost:8009/kankeri/

ErrorLog logs/kankeri-error_log
CustomLog logs/kankeri-access_log combined

SSLCertificateFile /etc/letsencrypt/live/kankeri.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/kankeri.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

80ポートや443ポートでのリクエストは、8009ポートでTomcat9に渡り、処理される。以下のURLでkankeriアプリにアクセス可能である。

http://kankeri.com/
https://kankeri.com/

複数のProxyPassを有効にするバーチャルホストの書き方

上記の状態で、TomcatのROOTページ、examples、docs、またユーザー定義のxxxアプリも扱えるように設定してみる。managerやhost-managerは8080ポートのままとして対象外とする。以下のように遷移させたい。

https://kankeri.com/           	--> kankeriアプリ
https://kankeri.com/tomcat  	--> TomcatのROOTページ
https://kankeri.com/docs  		--> Tomcatのdocs
https://kankeri.com/examples  	--> Tomcatのexamples
https://kankeri.com/xxx  		--> xxxアプリ

バーチャルホストを以下のように記述するとよい。

$ vi /etc/httpd/conf.d/vhost-02-kankeri-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost kankeri.com:443>
ServerName kankeri.com
ServerAlias www.kankeri.com
ServerAdmin webmaster@kankeri.com
DocumentRoot "/opt/tomcat9/webapps/kankeri"  	# DocumentRootにはkankeriを指定する。

# ユーザー定義のkankeriとxxxはDirectoryを定義する。
<Directory "/opt/tomcat9/webapps/kankeri">
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
<Directory "/opt/tomcat9/webapps/xxx">
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

ProxyPass /tomcat   ajp://localhost:8009/              # /tomcatならROOTに飛ばす
ProxyPass /examples ajp://localhost:8009/examples/
ProxyPass /docs     ajp://localhost:8009/docs/
ProxyPass /xxx      ajp://localhost:8009/xxx/
ProxyPass /         ajp://localhost:8009/kankeri/	   # スラッシュ(/)のみのProxyPassは最後に記述

ProxyPassReverse /xxx/ ajp://localhost:8009/xxx/

ErrorLog logs/kankeri-error_log
CustomLog logs/kankeri-access_log combined

SSLCertificateFile /etc/letsencrypt/live/kankeri.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/kankeri.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

記述上のポイントを説明する。

  • DocumentRootには、スラッシュ(/)で定義したいkankeriアプリを指定する。
  • ユーザー定義のkankeriアプリとxxxアプリには、Directoryを記述しておく。TomcatのROOTページ、examples、docsには、httpd.confのデフォルトを適用すればいいので、特にDirectoryの記述は不要である。
  • ProxyPass 記述では、ROOTディレクトリは特殊扱いなので、URLに"ROOT"は記述しない。
  • スラッシュ(/)のみのProxyPassは、最後に記述する。例では、kankeriアプリになる。上段からマッチングされるので順序が重要となる。
  • ProxyPassReverseには、xxxのみを記述する。

これにより、前述のURLでそれぞれのページに遷移可能になる。TomcatのROOTページ、examples、docsを公開する必要はないが、一例として代用した。

以上

../
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0