Time for another installment of stupid linux tips and tricks. Tonight I will give you a fun recipe for website proxying that can be used to pass sites to a backend host or just outright proxy to a different network like I did when I was moving and neglected to update my DNS with the right timeouts.
Okay so first things first you will need some apache modules loaded.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
These are the basic proxy modules. You will also need the virtual host module for this next bit of fun and it is.
LoadModule vhost_alias_module modules/mod_vhost_alias.so
Okay so now we have all the modules loaded. Onto the config! Apache must have a virtual host named before it will recognize any of the virtual host configs. Ubuntu and suse, I think, have a host already setup so this is the line you are looking for.
NameVirtualHost *:80 or NameVirtualHost *
The next part is purely preference but you will need to make your virtual hosts. I like to keep mine near their content so if I have a website at /home/website/www i'll put the config at /home/website/website.conf and then I would put an include at the bottom of my httpd.conf like include /home/website/website.conf. Whichever virtual host loads first will be the default website that answers anytime someone hits the IP instead of a name. You can also put them in your conf.d directory or if you are on ubuntu under enabled-sites.
<VirtualHost *:80>
Servername wilpig.org
ServerAlias www.wilpig.org
ServerAlias *.wilpig.com
ProxyPass / http://<destination ip>/
ProxyPassReverse / http://<destination ip>/
CustomLog /home/wilbur/logs/wilpig.org/access_log combined
ErrorLog /home/wilbur/logs/wilpig.org/error_log
</VirtualHost>
Alright here is an example so I will explain the bits and pieces. First you have to have the server name and you will want to specify any alternate names that you might want to use with this site as well. So my default site is wilpig.org and then I gave it the aliases of www.wilpig.org and *.wilpig.org. The *.wilpig.org is a catch all so I can point any ole name at it and it will resolve back to my main site. Next you have the proxy commands. They are relatively simple which instruct the server on how to rewrite the links to pull the content up correctly. Finally we have the log directives. I'm sure you are asking yourself why the heck I would log my proxy traffic and the simple answer is the webserver will show all the traffic as coming from the proxy instead of the real clients which makes the logs almost useless.
You can do some funny things with this setup like use a host name instead of an IP. I setup a few hosts for my change over in the hosts table. The proxy for my server move was set as http://wilpig.org/ and the server looked at the host table for the IP instead of me specifying it. By setting it up with the host name you can proxy a site to another virtual site if needed.
And that concludes this next installment of my stupid linux tricks.