ProxyPass, ProxyReverse vs AJP

user121196 picture user121196 · Jul 25, 2009 · Viewed 36.2k times · Source

I currently have a Tomcat + Apache HTTP server setting to serve my Java servlet:

ProxyPass /myservice http://localhost:8080/myservice
ProxyPassRerverse /myservice http://localhost:8080/myservice

This is all fine except that myservice needs to know the client IP address, which always turns out to be 127.0.0.1 due to the proxy. Is there a solution to get the real IP address? Is AJP an option?

doGet(HttpServletRequest request, HttpServletResponse response){
    request.getRemoteAddr()
}

Answer

caskey picture caskey · Jul 25, 2009

Do it like this:

in the apache config:

<Location /foo>
  ProxyPass ajp://localhost:8009/foo
  ProxyPassReverse ajp://localhost:8009/foo
</Location>

And then in your server.xml:

<Connector port="8009" 
           enableLookups="false" secure="true" URIEncoding="UTF-8"
           tomcatAuthentication="false"
           protocol="AJP/1.3" />

That should pass everything through. The AJP protocol passes the info, but http: doesn't.

You may not want secure="true", I use that because SSL is handled at the apache layer and I need tomcat to know that the connection should be considered a secure one.