0

I'm running a Java application on Apache. I'm trying to find a way to allow my app server code to know the hostname of the web server. Is there a way to do this?

Running

InetAddress addr = InetAddress.getLocalHost();
String hostname = addr.getHostName();

or

(HttpServletResponse)response.getHeader("Host");

both predictably yield the app server's hostname.

2 Answers2

0

Much depends on the configuration of your webservers, but I found that with Apache in front of JBoss, getRemoteHost returned the address of the Web Server.

Might be worth a go

DaveH
  • 7,187
  • 5
  • 32
  • 53
0

I have handled this in a few ways:

  1. Explicitly tell the app what host name it should be using via system properties
  2. Have a rewrite rule in Apache that adds a header to the request, such as X-originally-For
  3. Read the X-Forwarded-For header from the request

Note: I don't particularly like #3. Since the XFF header is passed host to host, you can't trust the addresses added by hosts outside your control.

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71