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()
}
Do it like this:
in the apache config:
And then in your server.xml:
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.
You can read the X-Forwarded-For in the request header.
From the Apache mod_proxy documentation:
In your servlet, you would have:
this is very simple: