Apache2 <-> Tomcat4.1 リバースプロキシ連携して、CGI環境変数 PATH_INFO がどんな値になるか調べる。
サーブレットAPI の javax.servlet.http.HttpServletRequest#getPathInfo() にて、PATH_INFO の値を取得することが可能。
OS は Debian GNU/Linux Sarge.
Apache2 のインストールとリバースプロキシ設定
# apt-get install apache2
# a2enmod proxy
Module proxy installed; run /etc/init.d/apache2 force-reload to enable.
# vi /etc/apache2/mods-available/proxy.conf
# cat /etc/apache2/mods-available/proxy.conf
<IfModule mod_proxy.c>
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /rp http://localhost:8180/ws
ProxyPassReverse /rp http://localhost:8180/ws
</IfModule>
# /etc/init.d/apache2 restart
Forcing reload of web server: Apache2.
Tomcat 4.1 のインストール
昔のメモを参考に。
-> Debian GNU/Linux Sarge に Tomcat 4.1 をインストールする
環境変数取得用JSP snoopy.jsp のソースコード
ほとんど Tomcat examples の snoop.jsp そのまま。
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<body>
<h1>snoopy どっぐ</h1>
<br>
JSP Request Method: <% out.print(request.getMethod()); %>
<br>
Request URI: <%= request.getRequestURI() %>
<br>
Request Protocol: <%= request.getProtocol() %>
<br>
Servlet path: <%= request.getServletPath() %>
<br>
Path info: <% out.print(request.getPathInfo()); %>
<br>
Query string: <% out.print(request.getQueryString()); %>
<br>
Content length: <%= request.getContentLength() %>
<br>
Content type: <% out.print(request.getContentType()); %>
<br>
Server name: <%= request.getServerName() %>
<br>
Server port: <%= request.getServerPort() %>
<br>
Remote user: <%= request.getRemoteUser() %>
<br>
Remote address: <%= request.getRemoteAddr() %>
<br>
Remote host: <%= request.getRemoteHost() %>
<br>
Authorization scheme: <%= request.getAuthType() %>
<br>
Locale: <%= request.getLocale() %>
<hr>
The browser you are using is <% out.print(request.getHeader("User-Agent")); %>
<hr>
</body>
</html>
Tomcat4.1 ウェブアプリ用 web.xml
<servlet>
<servlet-name>snoopy</servlet-name>
<jsp-file>snoopy.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>snoopy</servlet-name>
<url-pattern>/snoopy/*</url-pattern>
</servlet-mapping>
以下は、最初に間違えて設定していた、PATH_INFO が取得できない設定。
というか、http://luckyman:8180/ws/snoopy/hello みたいなアクセスが 404 not found になる。
こちらの場合、
servlet-mapping/url-pattern がイクない。
<servlet>
<servlet-name>snoopy</servlet-name>
<jsp-file>snoopy.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>snoopy</servlet-name>
<url-pattern>/snoopy</url-pattern>
</servlet-mapping>
Tomcat 4.1 へ直接アクセスした場合
WebBrowser -> Tomcat 4.1
PATH_INFOが取得できる設定にて、
URL: http://luckyman:8180/ws/snoopy/hello?a=b
snoopy どっぐ
JSP Request Method: GET
Request URI: /ws/snoopy/hello
Request Protocol: HTTP/1.1
Servlet path: /snoopy
Path info: /hello
Query string: a=b
Content length: -1
Content type: null
Server name: luckyman
Server port: 8180
Remote user: null
Remote address: 192.168.0.77
Remote host: 192.168.0.77
Authorization scheme: null
Locale: ja The browser you are using is Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8
Apache2 経由でアクセスした場合
WebBrowser -> Apache 2 -> Tomcat 4.1
URL: http://luckyman/rp/snoopy/hello?a=b
snoopy どっぐ
JSP Request Method: GET
Request URI: /ws/snoopy/hello
Request Protocol: HTTP/1.1
Servlet path: /snoopy
Path info: /hello
Query string: a=b
Content length: -1
Content type: null
Server name: localhost
Server port: 8180
Remote user: null
Remote address: 127.0.0.1
Remote host: 127.0.0.1
Authorization scheme: null
Locale: ja The browser you are using is Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8
参考
- Eclipse 3.2 + Tomcat4.1 の開発環境を整える & WEB-INF 以下のファイルを読み込む
- Apache + mod_proxy + mod_cache + mod_disk_cache によるリバースプロキシの導入
- Apache 1.3.36 と Tomcat 4.1 の連携 (Reverse Proxy)
実験用に環境をスクラップ&ビルドしまくっているので、この手の自分のメモはとても役立つ。何度もいつでもコピペインストール、コピペ設定ファイル作成。バージョンが異なる場合はちょっと注意が必要だけど。
tags: zlashdot Java Java Tomcat
Posted by NI-Lab. (@nilab)