I'm setting up Apache2 as a reverse proxy for a remote site. Let's assume the remote site is http://app.remotesite.com. Here is a snippet from my virtual host config:
ProxyPass /pxy/ http://app.remotesite.com/
So this should take a request like http://app.mysite.com/pxy/search?q=abc, and pass it through as http://app.remotesite.com/search?q=abc.
I am getting a "Bad request" when I try this. Based on the output in /var/log/apache2/error.log, it is doing the proxy correctly, but it looks like when it connects to the remote site it does so with its IP address. If I take that IP address (printed in error.log) and do a request with it, e.g. http://[IP address]/search?q=abc, I get the same "Bad request" error. My hypothesis is that the remote site is relying on the hostname to serve the request properly, but mod_proxy is not sending it over. I know about the ProxyPreserveHost setting, but this is for preserving the original hostname in the proxy request (in this case, app.mysite.com) which is not what I want.
Can anyone suggest a way for me to force mod_proxy to use the remote site's hostname in its request? Or, if my hypothesis does not make sense, point out what else might be going wrong?
Your hypothesis is probably incorrect.
mod_proxy
connects using the hostname you provide in the proxy URL.If you request
http://app.remotesite.com/search?q=abc
on the command line usingcurl
, do you get the response you expect? If so, then a good place to start is looking at the difference between the request that curl produces vs. the request thatmod_proxy
is sending over.To see what
curl
is doing, you can use the--trace-ascii <file>
option, like this:This will produce output in
trace.out
that looks something like:Getting the same information out of Apache is a little trickier; I would use
tcpdump
, which is a packet capturing tool. Start capturing packets like this:While tcpdump is running, make your request from a browser (or curl, or whatever), stop the
tcpdump
with^C
, and then examine the file like this:Which will get you something like:
This will show the URL being requested, the
Host:
header, and other useful information. See how it looks, and come back here if you don't spot something obvious.