<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>/root.eu &#187; howto</title>
	<atom:link href="http://slashroot.eu/category/howto/feed/" rel="self" type="application/rss+xml" />
	<link>http://slashroot.eu</link>
	<description>Notepad of geeky sysadmin</description>
	<lastBuildDate>Thu, 10 May 2012 16:41:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>HOWTO Massive virtual hosting on tomcat</title>
		<link>http://slashroot.eu/2010/01/07/howto-massive-virtual-hosting-on-tomcat/</link>
		<comments>http://slashroot.eu/2010/01/07/howto-massive-virtual-hosting-on-tomcat/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 07:33:54 +0000</pubDate>
		<dc:creator>root</dc:creator>
				<category><![CDATA[apache]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://slashroot.eu/?p=148</guid>
		<description><![CDATA[I found out today that tomcat virtual hosts are pretty lame. I thought that these all java based tomcat`s fancy plugins are much more powerful than old, simpleapache httpd. Well they aren`t. I wasn`t able to configure massive virtual hosting &#8230; <a href="http://slashroot.eu/2010/01/07/howto-massive-virtual-hosting-on-tomcat/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I found out today that tomcat virtual hosts are pretty lame. I thought that these all java based tomcat`s fancy plugins are much more powerful than old, <em>simple</em>apache httpd. Well they aren`t. I wasn`t able to configure massive virtual hosting for servlets. I wanted to run diffrent web application based on domain name that comes in URL. For example <code>myapp1.example.com</code> would run application <code>myapp1</code>, <code>myapp2.example.com</code> would run <code>myapp2</code>, etc. And there could be hundreds of them so I didn`t want to add hundreds of entries in <code>server.xml</code> config file. I`m a lazy person <img src='http://slashroot.eu/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>In order to configure virtual hosting based on above assumptions I used apache http server as a proxy to tomcat.<br />
First you need to add wildcard records to your domain (<code>example.com</code>) so that records of all its subdomains can be resolved to IP address of your server. Following record should be added to your bind server zone config:</p>
<pre>
*   IN   A   1.2.3.4
</pre>
<p>where <code>1.2.3.4</code> is IP address of your server.</p>
<p>Next you need to configure httpd server. Please make sure that you have ajp proxy module installed on your server, as connections to tomcat are based on AJP protocol. On CentOS/RHEL 5 this module is included in standard httpd package (see <code>/etc/httpd/conf.d/proxy_ajp.conf</code>).<br />
Now you need to create configuration for your virual hosts. I created a new file <code>/etc/httpd/conf.d/tomcat-vhosting.conf</code>:</p>
<pre>
UseCanonicalName Off
RewriteEngine On

# vhost map using perl script
RewriteMap vhost prg:/usr/local/bin/apache-getvhost.pl

# do no rewrite restricted names
RewriteCond %{SERVER_NAME} !^docs\.
RewriteCond %{SERVER_NAME} !^examples\.
RewriteCond %{SERVER_NAME} !^host-manager\.
RewriteCond %{SERVER_NAME} !^ROOT\.

# rewrite it
RewriteRule ^/(.*)$ ajp://localhost:8009/${vhost:%{SERVER_NAME}}/$1 [P]
</pre>
<p>Quite simple and cool, isn`t it? <img src='http://slashroot.eu/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  I`m sure that you probably expected <code>VirtualHost</code> directives, but all you need is a powerfull rewriting feature of apache. This configuration allows to access myapp application located in tomcat`s webapps directory via <code>http://myapp.example.com</code>.<br />
I used custom rewrite map which is a simple perl script. All it does is extract subdomain from server name based on URL.</p>
<p>Put the following in <code>/usr/local/bin/apache-getvhost.pl</code></p>
<pre>
#!/usr/bin/perl

$| = 1;

while (&lt;STDIN&gt;) {
  if (/(.*?)\.example\.com/)      {
      print $1."\n";
  } else {
      print $_."\n";
  }
}
</pre>
<p>and make it executable</p>
<pre>
chmod +x /usr/local/bin/apache-getvhost.pl
</pre>
<p>Now all you need to do is provide some applications to tomcat.</p>
]]></content:encoded>
			<wfw:commentRss>http://slashroot.eu/2010/01/07/howto-massive-virtual-hosting-on-tomcat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

