request.getLocalAddr() does not return an IP-address, but the host name from the http header.
Accessing http://127.0.0.1:8080/ will make getLocalAddr() return 127.0.0.1
Accessing http://localhost:8080/ will make getLocalAddr() return localhost
Accessing http://localhost/ using mod_jk/ajp13 will also make getLocalAddr() return localhost
I see this on java7 using MacOS and FreeBSD.

Hi,
I can confirm that this problem happens with Tomcat 8.0.9 when using AJP NIO connector (I used the ISAPI Redirector with IIS).
In this case, request.getLocalAddr() returns the name of the "Host" header instead of an IP address, e.g. "localhost".
If using the HTTP NIO connector, the problem does not occur; request.getLocalAddr() will return an IP address in this case, e.g. "0:0:0:0:0:0:0:1".

All AJP connectors behave this way and have done since getLocalAddr() was added in Servlet 2.4.
The problem is that the AJP protocol passes the remoteAddr, remoteHost, localName and localPort but *NOT* the localAddr.
localAddr should be the IP address the client used to contact the server and this simply is not available - hence why the host name was used.
Possible solutions include modifying the AJP protocol but this is non-trivial.
A simple work-around would be to have the reverse proxy add a custom header with the localAddr information and then have a Valve / Filter read that header value and then present it to the application.
I'm leaning towards resolving this as WONTFIX.

Technically, this shouldn't require a change to the AJP protocol, as arbitrary HTTP headers can be sent just by modifying the configuration.
Unfortunately, the CGI spec does not specify anything like "localAddr"... the closest thing available is SERVER_NAME which ... comes from the client's "Host" header. I suspect this is what is being used for getLocalAddr, here.
Reading a bit about httpd's capabilities, I'm not sure it's actually possible to get the IP address of the server interface that accepted the request from the client. If the address *can* be obtained, sending it via an HTTP header and using something like the RemoteIpValve would probably work.

I've just taken a look at the httpd docs as well and I don't see any way of obtaining the IP address of the interface that the request was received on. That reduces the choices to:
a) returning null
b) return the hose name (current behaviour)
c) use DNS to get the IP address
d) return an empty string
If I was starting from a clean slate, I choose option a). However, given the current behaviour that is likely to start triggering NPEs in applications. That makes d) preferrable to a).
There are many cases where c) will not return the correct value. DNS round-robin load-balancing being one. Therefore, I don't think this apporach should be used.
So that leaves b) or d) as the possible options in this case. I don't think we should change it for 7.0.x or earlier but we could change it in 8.0x onwards.
I leave it a few days before doing anything to give folks a chance to comment.

As examples:
Implementation of custom "JK_LB_ACTIVATION" request attribute: r1078846
Implementation of custom "AJP_REMOTE_PORT" request attribute: r756926r757223
Those are the only custom header names in native/common/jk_ajp_common.h of mod_jk, so I think those are the only examples.

OK. Looks like the information is available in the httpd internals but not (obviously) via config. Therefore, if mod_jk implements a new request atribute to pass this information, Tomcat can use it for getLocalAddr(). Moving this to mod_jk to get that implemented.

I added support for the new attribute to TC 8 in r1609593.
Needs some more testing before porting back to TC7 an probably 6.
A full test needs a dev build of mod_jk r1609589 or later.
Alternatively one can try in Apache:
SetEnvIf Server_Addr "(.*)" AJP_LOCAL_ADDR=$1
JkEnvVar AJP_LOCAL_ADDR
Which for current releases of mod_jk would send a request attribute named AJP_LOCAL_ADDR with its value set to the value of the Apache internal variable SERVER_ADDR which in turn contains the local IP address of Apache.
Starting with mod_jk 1.2.41 the attribute will be set without explicit configuration.

Forwarding the local IP address as the custom request attribute will also be part of mod_proxy_ajp in the Apache web server 2.4.10. Adding it to the next release of Apache httpd 2.2 has been proposed as well.

(In reply to Eugene Chung (TmaxSoft) from comment #18)
> I'm a bit confused with the definition of getLocalXXX() APIs.
> Should they return the web server(intermediary in view of WAS)'s information?
I haven't checked the Spec but I don't it will clarify the behavior for that situation.
One of the major ideas behind the AJP protocol is to transport the communication data, that the intermediary saw, to the backend and let the application see that information as if the backend was placed at the position of the intermediary. One goal is to make sure, that any self-referential URL the application creates will actually point to the intermediary and not the the backend.
That's why we have chosen to return the local IP of the intermediary from the AJP connector.

This is ASF Bugzilla: the Apache Software Foundation bug system. In case
of problems with the functioning of ASF Bugzilla, please contact
bugzilla-admin@apache.org.
Please Note: this e-mail address is only for reporting problems
with ASF Bugzilla. Mail about any other subject will be silently
ignored.