<?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>Alfa Jango Blog &#187; Apache</title>
	<atom:link href="http://www.alfajango.com/blog/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alfajango.com/blog</link>
	<description>Engineering, Software, and Entrepreneurship</description>
	<lastBuildDate>Thu, 09 Sep 2010 17:52:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Caching, Zipping, and (Amazon CloudFront) CDN For A Rails App</title>
		<link>http://www.alfajango.com/blog/caching-zipping-and-cdn-for-a-rails-app/</link>
		<comments>http://www.alfajango.com/blog/caching-zipping-and-cdn-for-a-rails-app/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 21:09:47 +0000</pubDate>
		<dc:creator>steve</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Amazon CloudFront]]></category>
		<category><![CDATA[Amazon S3]]></category>
		<category><![CDATA[assets]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Components]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.alfajango.com/blog/?p=447</guid>
		<description><![CDATA[In this article, we're going to speed up our Rails application by up to 75%, simply by optimizing our Rails asset host. We're going to serve our components (stylesheets, javascripts, images, etc.) from a combination of our app's server and Amazon CloudFront (Option A, recommended), or entirely from CloudFront (Option B - easier). ]]></description>
			<content:encoded><![CDATA[<div class="note">
<p>
<em>This is Article #4 of a 4-part series. For a good primer, check out the first two articles listed below. For the reasoning and analysis behind the &#8220;Recommended&#8221; option in this article, check out Part 3, <a href="how-to-combine-gzip-plus-cdn-for-fastest-page-loads/">How to Combine GZip + CDN for Fastest Page Loads</a>. Otherwise, jump right in!</em>
</p>
<ul>
	<li><a href="is-your-site-too-slow-the-importance-of-page-load-speed/">The Importance of Page Load Speed</a></li>
	<li><a href="improve-page-load-speed-by-improving-component-load-speed/">Improve Page Load Speed (by 80%) by Improving Component Load Speed</a></li>
	<li><a href="how-to-combine-gzip-plus-cdn-for-fastest-page-loads/">How to Combine GZip + CDN for Fastest Page Loads</a></li>
	<li><strong>Caching, Zipping, and (Amazon CloudFront) CDN For A Rails App</strong>
<ul>
        <li>Prerequisites
        <ol>
          <li>Cached stylesheets and javascripts</li>
          <li>Creating an Amazon AWS Account</li>
        </ol></li>
	<li>Setup S3 Buckets</li>
	<li>Setup CloudFront Distributions</li>
	<li>Create CNAME records (optional)</li>
	<li>Install Rails S3 Synch Plugin
        <ol>
          <li>Installing AWS-S3 Gem</li>
          <li>Configure S3 Synch Plugin</li>
          <li>Add S3 Synch to Deployment</li>
        </ol></li>
	<li>Option A: Compressible Assets from App Server, Images from CloudFront (recommended)
        <ol>
          <li>Configure Rails Asset Host</li>
          <li>Create A-name Record</li>
          <li>Configure Apache</li>
        </ol></li>
	<li>Option B: Serve Everything from CloudFront (easier, but not recommended)
        <ol>
          <li>Configure Rails Asset Host</li>
          <li>Pre-compile Cached Stylesheet and Javascript File</li>
        </ol></li>
        <li>Conclusion</li>
</ul>
</li>
</ul>
</div>
<h2></h2>
<p>
In this article, we&#8217;re going to speed up our Rails application by up to 75%, simply by optimizing our Rails asset host. We&#8217;re going to serve our components (stylesheets, javascripts, images, etc.) from a combination of our app&#8217;s server and Amazon CloudFront (Option A, recommended), or entirely from CloudFront (Option B &#8211; easier). 
</p><p>
The best option for you may depend on your specific needs, but I&#8217;ll cover both processes below. For a an in-depth analysis of why Option A is recommended over Option B, see the last article in this series, <a href="how-to-combine-gzip-plus-cdn-for-fastest-page-loads/">How to Combine GZip + CDN for Fastest Page Loads</a>.
</p>
<h2>Prerequisites</h2>
<h3>Cached Stylesheets and Javascripts</h3>
<p>
Another way to reduce page load time is to combine all of your components into as few files as possible. In other words, combine all of your stylesheets into a single css file, and likewise with your javascripts. Remember from the last article, that each request takes 50-150ms, not including the response and download time. If you have 10 separate javascripts, this equates to 0.5-1.5 seconds just to request the files (not to mention all the time to download them). If you can combine all of the files into one, that means you need just one request to get the same amount of data.
</p>
<p>
Luckily in Rails, this is easy, simply add <code class="codecolorer rails default"><span class="rails"><span style="color:#ff3333; font-weight:bold;">:cache</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'cached-file-name'</span></span></code> to your <code class="codecolorer rails default"><span class="rails"><span style="color:#5A0A0A; font-weight:bold;">stylesheet_link_tag</span></span></code> and <code class="codecolorer rails default"><span class="rails"><span style="color:#5A0A0A; font-weight:bold;">javascript_include_tag</span></span></code> in your application layout. For example:
</p>
<p>

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">stylesheet_link_tag</span> <span style="color:#996600;">'reset'</span>, <span style="color:#996600;">'application'</span>, <span style="color:#ff3333; font-weight:bold;">:cache</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'all-app-stylesheets'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">javascript_include_tag</span> <span style="color:#996600;">'jquery'</span>, <span style="color:#996600;">'jquery-ui'</span>, <span style="color:#996600;">'application'</span>, <span style="color:#ff3333; font-weight:bold;">:cache</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'all-app-javascripts'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></div>

</p>
<p>
Now, as long as the following line is set to true in your environment.rb, or more likely in production.rb, Rails will either load your combined files in the layout, or create and load them if they don&#8217;t already exist.
</p><p>

<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">perform_caching</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color:#0000FF; font-weight:bold;">true</span></div></div>

</p>
<p>
<strong>Simply packing all stylesheets and javascripts into one file each reduced page load time of one of our production applications from 10.1 to 8.3 seconds (an 18% reduction in load time alone).</strong>
</p>
<h3>Creating an Amazon AWS Account</h3>
<p>
If you do not yet have an Amazon AWS account, you will need to create that and enable S3 and CloudFront services. See <a href="http://www.hongkiat.com/blog/amazon-s3-the-beginners-guide/">this writeup on creating and setting up an Amazon S3 account</a>. for helpful instructions.
</p>
<h2>Setup S3 Buckets</h2>
<p>
Once you&#8217;ve signed up for your Amazon AWS account and activated S3 and CloudFront, you&#8217;ll want to setup 4 S3 buckets for your application, using Amazons S3 management console.
</p><p>
We&#8217;re going to setup 4 buckets and CDN distributions because some old browsers still have an artificial limitation that only allow 2 concurrent connections to each domain, meaning our components will take longer to download from Amazon if they can only be downloaded 2 at a time. By creating 4 different domains pointing to 4 different buckets/distributions, we&#8217;re allowing our components to download up to 8 at a time from those browsers that still enforce this limitation.
</p>
<p>
<img src="http://www.alfajango.com/blog/wp-content/uploads/2010/06/amazon-s3-bucket-creation-e1276805768507.jpg" alt="" title="amazon-s3-bucket-creation" width="600" height="307" class="alignnone size-full wp-image-583" />
</p>
<div class="in-depth">
When naming your S3 buckets, avoid using periods if you would like the option of accessing your components directly from S3 over HTTPS. Amazon has a trusted SSL wildcard certificate for *.s3.amazonaws.com. 
<br /><br />
If you name your bucket <em>cdn0.yourapp.com</em>, then your components will have the URL <em>https://cdn0.yourapp.com.s3.amazonaws.com/stylesheet.css</em>. This will give you a warning message saying the connection is not trusted, because the browser treats your bucket name as subdomains (and in this case, <em>com.s3.amazonaws.com</em> would be trusted, but subdomains of that, <em>cdn0.yourapp</em> and <em>yourapp</em> will not).
</div>
<h2>Setup CloudFront Distributions</h2>
<p>
Once your S3 buckets are created, click over to the CloudFront tab and create one distribution for each S3 bucket as shown. You can type any comment to help you quickly identify each distribution.
</p>
<p>
<img src="http://www.alfajango.com/blog/wp-content/uploads/2010/06/amazon-cloudfront-distribution-creation-e1276805828838.jpg" alt="" title="amazon-cloudfront-distribution-creation" width="600" height="390" class="alignnone size-full wp-image-584" />
</p>
<h2>Create CNAME Records (optional)</h2>
<p>
Once you&#8217;ve created your 4 CloudFront distributions, you may create a CNAME record for each distribution. This allows you to serve files from CloudFront using your own asset subdomains, like <em>cdn0.yourapp.com</em>, instead of <em>raNDomString1234.cloudfront.net</em>. We&#8217;ll use the following format of <code class="codecolorer ruby default"><span class="ruby">cdn<span style="color:#006600; font-weight:bold;">%</span>d.<span style="color:#9900CC;">yourapp</span>.<span style="color:#9900CC;">com</span></span></code>, where <code class="codecolorer ruby default"><span class="ruby"><span style="color:#006600; font-weight:bold;">%</span>d</span></code> stands for digits 0-3:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cdn0.yourapp.com<br />
cdn1.yourapp.com<br />
cdn2.yourapp.com<br />
cdn3.yourapp.com</div></div>

</p><p>
<img src="http://www.alfajango.com/blog/wp-content/uploads/2010/06/amazon-cloudfront-cname-configuration.png" alt="" title="amazon-cloudfront-cname-configuration" width="334" height="557" class="alignnone size-full wp-image-580" />
</p>
<h2>Install Rails S3 Synch Plugin</h2>
<p>
This plugin adds some Capistrano recipes to synch our application&#8217;s <code class="codecolorer text default"><span class="text">public</span></code> directory with our four S3 buckets automatically every time we deploy our app. See <a href="http://spattendesign.com/2007/11/6/synching-your-amazon-s3-asset-host-using-capistrano">Spatten Design&#8217;s documentation</a> for more information. I&#8217;ve made some updates to their original plugin to properly set the Cache-control and Expires headers for our assets on S3, as well as to properly set the Content-encoding header for Gzipped assets.
</p>
<p class="note">Update: I&#8217;ve updated the S3 Synch Plugin further; it can now handle unique S3 buckets for different Rails environments (e.g. one set of buckets for production and another for staging). Be sure to update your synch_s3_asset_host.yml file as shown below.</p>
<p>

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">.<span style="color:#006600; font-weight:bold;">/</span>script<span style="color:#006600; font-weight:bold;">/</span>plugin install git:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>JangoSteve<span style="color:#006600; font-weight:bold;">/</span>synch_s3_asset_host.<span style="color:#9900CC;">git</span></div></div>

</p>
<h3>Installing AWS-S3 Gem</h3>
<p>The synch_s3_asset_host plugin requires the AWS-S3 gem, so add the following to your <code class="codecolorer ruby default"><span class="ruby">environment.<span style="color:#9900CC;">rb</span></span></code>:</p>
<p>

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;aws-s3&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;aws/s3&quot;</span></div></div>

</p><p>
&#8230;and then run the following from the terminal to install the S3 Synch plugin&#8217;s gem dependency:
</p><p>

<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> rake gems:<span style="color: #c20cb9; font-weight: bold;">install</span></div></div>

</p>
<h3>Configure S3 Synch Plugin</h3>
<p>
Create a <code class="codecolorer text default"><span class="text">config/synch_s3_asset_host.yml</span></code> file like this:
</p><p>

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">AWS_ACCESS_KEY_ID: <span style="color:#996600;">'YOURKEYHERE'</span><br />
AWS_SECRET_ACCESS_KEY: <span style="color:#996600;">'YourSecretAccessKeyHere'</span><br />
production:<br />
&nbsp; asset_host_name: <span style="color:#996600;">&quot;yourapp-com-cdn%d&quot;</span> <span style="color:#008000; font-style:italic;"># This is whatever you named your S3 buckets, using %d in place of the numbers 0-3</span><br />
<span style="color:#008000; font-style:italic;"># dry_run: false # Set to true if you want to test the asset_host uploading without doing anything on Amazon S3</span></div></div>

</p>
<p class="note">Update: The &#8220;production&#8221; part in the file above has been added for my latest update of the S3 Asset Synch Plugin.</p>
<h3>Add S3 Synch to Deployment</h3>
<p>
Now, in your Capistrano <code class="codecolorer text default"><span class="text">deploy.rb</span></code> script, add the following line to the <code class="codecolorer ruby default"><span class="ruby"><span style="color:#ff3333; font-weight:bold;">:deploy</span></span></code> namespace:
</p><p>

<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">namespace <span style="color:#ff3333; font-weight:bold;">:deploy</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; ...<br />
&nbsp; <span style="color:#9900CC;">before</span> <span style="color:#996600;">&quot;deploy:symlink&quot;</span>, <span style="color:#996600;">&quot;s3_asset_host:synch_public&quot;</span><br />
&nbsp; ...<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

</p><p>
&#8230;and then add the <code class="codecolorer ruby default"><span class="ruby"><span style="color:#ff3333; font-weight:bold;">:asset_host_syncher</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span></span></code> flag to the <code class="codecolorer ruby default"><span class="ruby"><span style="color:#ff3333; font-weight:bold;">:web</span></span></code> role:
</p><p>

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...<br />
<span style="color:#9900CC;">role</span> <span style="color:#ff3333; font-weight:bold;">:web</span>, <span style="color:#996600;">&quot;yourapp.com&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:asset_host_syncher</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><br />
...</div></div>

</p>
<h2>Option A: Compressible Assets from App Server, Images from CloudFront (recommended)</h2>
<p>
For more detail about why this method is recommended, <a href="how-to-combine-gzip-plus-cdn-for-fastest-page-loads/">see the last article in this series</a>.
</p>
<h3>Configure Rails Asset Host</h3>
<p>
Use the following configuration in your <code class="codecolorer text default"><span class="text">production.rb</span></code> file to configure the way Rails writes the URLs for asset_tags:
</p><p>

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;"># Enable serving of images, stylesheets, and javascripts from an asset server</span><br />
<span style="color:#008000; font-style:italic;"># config.action_controller.asset_host = &quot;http://assets.example.com&quot;</span><br />
<span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActionController::Base</span></span>.<span style="color:#9900CC;">asset_host</span> = <span style="color:#CC00FF; font-weight:bold;">Proc</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>source, request<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># the following will route to Amazon S3 + CloudFront if /images asset (setup with CNAMEs as domains cdn0-cdn3)</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># &nbsp; and will route to cdn for anything else (js, css, html), which routes to RMSR's own server so that files can be gzipped and served</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> source.<span style="color:#9900CC;">starts_with</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/images'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">unless</span> request.<span style="color:#9900CC;">ssl</span>? <span style="color:#008000; font-style:italic;"># CloudFront does not support HTTPS, but S3 does</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;http://cdn#{source.hash % 4}.yourapp.com&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#008000; font-style:italic;"># For SSL we want the certificate to match the hosting domain for cloudfront</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">&quot;https://yourcloudfrontdist0.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;https://yourcloudfrontdist1.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;https://yourcloudfrontdist2.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;https://yourcloudfrontdist3.cloudfront.net&quot;</span> <span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>source.<span style="color:#9900CC;">hash</span> <span style="color:#006600; font-weight:bold;">%</span> 4<span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># use the cahed and zipped subdomain for assets that can be zipped (i.e. non-binary filetypes)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># =&gt; text/html text/css application/x-javascript application/javascript</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;#{request.protocol}cache.yourapp.com&quot;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></div>

</p>
<div class="note">
If you did not configure custom CNAME records earlier, your Rails asset_host configuration would be a bit simpler:
<br /><br />

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActionController::Base</span></span>.<span style="color:#9900CC;">asset_host</span> = <span style="color:#CC00FF; font-weight:bold;">Proc</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>source, request<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> source.<span style="color:#9900CC;">starts_with</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/images'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">&quot;#{request.protocol}yourcloudfrontdist0.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;#{request.protocol}yourcloudfrontdist1.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;#{request.protocol}yourcloudfrontdist2.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;#{request.protocol}yourcloudfrontdist3.cloudfront.net&quot;</span> <span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>source.<span style="color:#9900CC;">hash</span> <span style="color:#006600; font-weight:bold;">%</span> 4<span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># use the cahed and zipped subdomain for assets that can be zipped (i.e. non-binary filetypes)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># =&gt; text/html text/css application/x-javascript application/javascript</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;#{request.protocol}cache.yourapp.com&quot;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></div>

</div>
<div class="in-depth">
Notice the <code class="codecolorer rails default"><span class="rails">source.<span style="color:#9900CC;">hash</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">4</span></span></code> code above. This ensures that the same component is always served from the same subdomain to take full advantage of client-side caching for that component, rather than randomly selecting from which subdomain to serve each component on each page load.
<br /><br />
For more information on configuring Rails&#8217;s asset_host, <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html">see the documentation for <code class="codecolorer ruby default"><span class="ruby">Base.<span style="color:#9900CC;">asset_host</span></span></code></a>
</div>
<h3>Create A-name Record</h3>
<p>We will also need to create an A-name record for the <i>cache.yourapp.com</i> subdomain, which points to your application server&#8217;s IP address.</p>
<h3>Configure Apache</h3>
<p>
Now we need to configure Apache to accept incoming requests to our &#8220;cache&#8221; subdomain, setting the appropriate far-future Expires and Cache-control headers. We also need to tell Apache to automatically compress and serve any compressible filetype on the fly. Add this to your site&#8217;s Apache <code class="codecolorer text default"><span class="text">conf</span></code> file:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...<br />
&nbsp; &nbsp;<span style="color: #adadad; font-style: italic;"># gzip html, css, and js</span><br />
&nbsp; &nbsp;<span style="color: #00007f;">AddOutputFilterByType</span> DEFLATE text/html text/css application/x-javascript application/javascript<br />
<br />
&nbsp; &nbsp;&lt;<span style="color: #000000; font-weight:bold;">virtualhost</span> *:80&gt;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #00007f;">ServerName</span> cache.yourapp.com<br />
&nbsp; &nbsp; &nbsp; <span style="color: #00007f;">DocumentRoot</span> /path/to/yourapp/public<br />
&nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight:bold;">filesmatch</span> <span style="color: #7f007f;">&quot;.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$&quot;</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00007f;">ExpiresActive</span> <span style="color: #0000ff;">On</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00007f;">ExpiresDefault</span> <span style="color: #7f007f;">&quot;access plus 1 year&quot;</span><br />
&nbsp; &nbsp; &nbsp; &lt;/<span style="color: #000000; font-weight:bold;">filesmatch</span>&gt;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #00007f;">FileETag</span> <span style="color: #0000ff;">none</span><br />
&nbsp; &nbsp;&lt;/<span style="color: #000000; font-weight:bold;">virtualhost</span>&gt;<br />
...</div></div>

</p>
<div class="in-depth">
Also note here that we turned off the ETag functionality for this subdomain. ETags (&#8220;entity tags&#8221;) are suppose to be a more flexible mechanism to query and invalidate cached assets, rather than using the last-modified_date of the file. <a href="http://developer.yahoo.com/performance/rules.html#etags">See Yahoo&#8217;s ETag description</a> for more info.
<br /><br />
However, the ETag&#8217;s uniqueness depends not just on the file, but usually on the server it&#8217;s being served from as well. This means if you have your assets copied to several asset domains on different servers, a file downloaded and cached from one server, and then the next page tries to pull the asset from another asset domain, the file&#8217;s ETag will not match the ETag of the cached file, so it will re-download the file instead of serving it from cache.
<br /><br />
Furthermore, Rails does a very good job of appending the last-modified-date to the asset file names (using the asset_tag helpers), which effectively serves, caches, and invalidates the assets for you as necessary. So, we&#8217;re much better off just turning ETags off for our Rails app.
</div>
<p>
Now we need to make sure the appropriate Apache modules are enabled and restart Apache.
</p><p>

<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> a2enmod deflate<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> a2enmod expires<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 force-reload</div></div>

</p>
<h2>Option B: Serve Everything from CloudFront (easier, but not recommended)</h2>
<p>
For more detail about why this is not recommended, <a href="how-to-combine-gzip-plus-cdn-for-fastest-page-loads/">see the last article in this series</a>. Basically, though, it&#8217;s because it requires you to make one of the following compromises:
</p>
<ul>
<li>a) Serve all files uncompressed, resulting in file sizes up to 4x bigger than necessary.</li>
<li>b) Serve Gzipped assets from CloudFront without first detecting whether or not the visitor&#8217;s browser support Gzip encoding.</li>
</ul>
<p>
That being said, if this is acceptable for you, this method is simpler to set up and configure.
</p>
<h3>Configure Rails Asset Host</h3>
<p>Add the following to your <code class="codecolorer text default"><span class="text">production.rb</span></code>:</p>
<p>

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActionController::Base</span></span>.<span style="color:#9900CC;">asset_host</span> = <span style="color:#CC00FF; font-weight:bold;">Proc</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>source, request<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># Enable serving of images, stylesheets, and javascripts from an asset server</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># config.action_controller.asset_host = &quot;http://assets.example.com&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">unless</span> request.<span style="color:#9900CC;">ssl</span>? <span style="color:#008000; font-style:italic;"># CloudFront does not support HTTPS, but S3 does</span><br />
&nbsp; &nbsp; <span style="color:#996600;">&quot;http://cdn#{source.hash % 4}.yourapp.com&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#008000; font-style:italic;"># For SSL we want the certificate to match the hosting domain for cloudfront</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">&quot;https://yourcloudfrontdist0.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;https://yourcloudfrontdist1.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;https://yourcloudfrontdist2.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#996600;">&quot;https://yourcloudfrontdist3.cloudfront.net&quot;</span> <span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>source.<span style="color:#9900CC;">hash</span> <span style="color:#006600; font-weight:bold;">%</span> 4<span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div></div>

</p>
<div class="note">
Again, if you did not configure custom CNAME records earlier, your Rails asset_host will be a bit simpler:
<br /><br />

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActionController::Base</span></span>.<span style="color:#9900CC;">asset_host</span> = <span style="color:#CC00FF; font-weight:bold;">Proc</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>source, request<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">&quot;#{request.protocol}yourcloudfrontdist0.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; <span style="color:#996600;">&quot;#{request.protocol}yourcloudfrontdist1.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; <span style="color:#996600;">&quot;#{request.protocol}yourcloudfrontdist2.cloudfront.net&quot;</span>,<br />
&nbsp; &nbsp; <span style="color:#996600;">&quot;#{request.protocol}yourcloudfrontdist3.cloudfront.net&quot;</span> <span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>source.<span style="color:#9900CC;">hash</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div></div>

</div>
<h3>Pre-compile Cached Stylesheet and Javascript File</h3>
<p>
If you&#8217;re serving every component from CloudFront, you will need to pre-compile your stylesheets and javascripts on every deploy. Otherwise, Rails will try to compile and save the files to your application server, but try to serve them from S3 (where they won&#8217;t exist).
</p><p>
To solve this, we&#8217;ll add some Capistrano scripts to our <code class="codecolorer text default"><span class="text">deploy.rb</span></code> to compile our files for us before the synch_s3_asset_host plugin copies our <code class="codecolorer text default"><span class="text">public</span></code> directory over to our S3 buckets. But this means, we&#8217;d have to copy the list of asset files to be compiled into our Capistrano script, as well as having them listed in our <code class="codecolorer text default"><span class="text">application.html.erb</span></code> layout. To DRY things up a little, we&#8217;re going to create some project-wide constants:
</p><p>
<code class="codecolorer text default"><span class="text">lib/assets_for_cache.rb</span></code>
</p><p>

<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">module</span> AssetsForCache<br />
&nbsp; &nbsp;JAVASCRIPT_FILES = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'jquery'</span>, <span style="color:#996600;">'jquery-ui'</span>, <span style="color:#996600;">'application'</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp;STYLESHEET_FILES = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'reset'</span>, <span style="color:#996600;">'application'</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp;JAVASCRIPT_CACHE_FILE = <span style="color:#996600;">'all-app-javascripts'</span><br />
&nbsp; &nbsp;STYLESHEET_CACHE_FILE = <span style="color:#996600;">'all-app-stylesheets'</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

</p><p>
And then replace your <code class="codecolorer ruby default"><span class="ruby">javascript_include_tag</span></code> and <code class="codecolorer text default"><span class="text">stylesheet_link_tag</span></code> in your application layout with the following:
</p><p>

<div class="codecolorer-container rails twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">javascript_include_tag</span> <span style="color:#6666ff; font-weight:bold;">AssetsForCache::JAVASCRIPT_FILES</span>, <span style="color:#ff3333; font-weight:bold;">:cache</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#6666ff; font-weight:bold;">AssetsForCache::JAVASCRIPT_CACHE_FILE</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">stylesheet_link_tag</span> <span style="color:#6666ff; font-weight:bold;">AssetsForCache::STYLESHEET_FILES</span>, <span style="color:#ff3333; font-weight:bold;">:cache</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#6666ff; font-weight:bold;">AssetsForCache::STYLESHEET_CACHE_FILE</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></div>

</p><p>
Add this to your deploy.rb script:
</p><p>

<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:300px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">namespace <span style="color:#ff3333; font-weight:bold;">:assets</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp;<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/../lib/assets_for_cache.rb'</span><br />
&nbsp; &nbsp;set <span style="color:#ff3333; font-weight:bold;">:stylesheets</span>, <span style="color:#6666ff; font-weight:bold;">AssetsForCache::STYLESHEET_FILES</span><br />
&nbsp; &nbsp;set <span style="color:#ff3333; font-weight:bold;">:javascripts</span>, <span style="color:#6666ff; font-weight:bold;">AssetsForCache::JAVASCRIPT_FILES</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;task <span style="color:#ff3333; font-weight:bold;">:package_cached_assets</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; package_stylesheets<br />
&nbsp; &nbsp; &nbsp; package_javascripts<br />
&nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;task <span style="color:#ff3333; font-weight:bold;">:package_stylesheets</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:web</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp;sudo <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#123;</span>rm <span style="color:#006600; font-weight:bold;">-</span>f <span style="color:#008000; font-style:italic;">#{release_path}/public/stylesheets/#{AssetsForCache::STYLESHEET_CACHE_FILE}.css}</span><br />
&nbsp; &nbsp; &nbsp;stylesheets.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>stylesheet<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;run <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#123;</span>cat <span style="color:#008000; font-style:italic;">#{release_path}/public/stylesheets/#{stylesheet}.css &gt;&gt; \</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#008000; font-style:italic;">#{release_path}/public/stylesheets/#{AssetsForCache::STYLESHEET_CACHE_FILE}.css}</span><br />
&nbsp; &nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp;run <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#123;</span>gzip <span style="color:#006600; font-weight:bold;">-</span>c <span style="color:#008000; font-style:italic;">#{release_path}/public/stylesheets/#{AssetsForCache::STYLESHEET_CACHE_FILE}.css &gt; #{release_path}/public/stylesheets/#{AssetsForCache::STYLESHEET_CACHE_FILE}.css.gz}</span><br />
&nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp;task <span style="color:#ff3333; font-weight:bold;">:package_javascripts</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:web</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp;sudo <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#123;</span>rm <span style="color:#006600; font-weight:bold;">-</span>f <span style="color:#008000; font-style:italic;">#{release_path}/public/javascripts/#{AssetsForCache::JAVASCRIPT_CACHE_FILE}.js}</span><br />
&nbsp; &nbsp; &nbsp;javascripts.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>javascript<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;run <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#123;</span>cat <span style="color:#008000; font-style:italic;">#{release_path}/public/javascripts/#{javascript}.js &gt;&gt; \</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#008000; font-style:italic;">#{release_path}/public/javascripts/#{AssetsForCache::JAVASCRIPT_CACHE_FILE}.js}</span><br />
&nbsp; &nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp;run <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#123;</span>gzip <span style="color:#006600; font-weight:bold;">-</span>c <span style="color:#008000; font-style:italic;">#{release_path}/public/javascripts/#{AssetsForCache::JAVASCRIPT_CACHE_FILE}.js &gt; #{release_path}/public/javascripts/#{AssetsForCache::JAVASCRIPT_CACHE_FILE}.js.gz}</span><br />
&nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

</p><p>
&#8230;and then add this to the <code class="codecolorer ruby default"><span class="ruby"><span style="color:#ff3333; font-weight:bold;">:deploy</span></span></code> namespace in your <code class="codecolorer text default"><span class="text">deploy.rb</span></code> file, before calling the s3_asset_host sync script:
</p><p>

<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">namespace <span style="color:#ff3333; font-weight:bold;">:deploy</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; ...<br />
&nbsp; <span style="color:#9900CC;">before</span> <span style="color:#996600;">&quot;deploy:symlink&quot;</span>, <span style="color:#996600;">&quot;assets:package_cached_assets&quot;</span><br />
&nbsp; before <span style="color:#996600;">&quot;deploy:symlink&quot;</span>, <span style="color:#996600;">&quot;s3_asset_host:synch_public&quot;</span><br />
&nbsp; ...<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>

</p>
<h2>Conclusion</h2>
<p>
Now simply save your project and deploy it! The first deploy will take quite a while, as your entire /public directory will be copied to all 4 buckets on Amazon S3, one at a time. But after that, it&#8217;s a painless process. 
</p>
<div class="in-depth">
If you have any files or directories in your public folder that are not assets to be copied to S3 (like a WordPress blog or whatever), you can add them to the <code class="codecolorer text default"><span class="text">--exclude</span></code> list in the synch_s3_asset_host plugin on line 186 of <code class="codecolorer text default"><span class="text">vendor/plugins/synch_s3_asset_host/recipes/synch_s3_asset_host.rb</span></code>
</div>
<p>
Whether you chose the &#8220;recommended&#8221; or the &#8220;easier&#8221; option, you should immediately notice a significant increase in the performance of your Rails app. Thanks for sticking with me through this 4-part series! Please let me know if you have any thoughts, questions, or feedback in the comments.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.alfajango.com/blog/caching-zipping-and-cdn-for-a-rails-app/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Make Sure Your Rails Application is Actually Caching (and not just pretending)</title>
		<link>http://www.alfajango.com/blog/make-sure-your-rails-application-is-actually-caching-and-not-just-pretending/</link>
		<comments>http://www.alfajango.com/blog/make-sure-your-rails-application-is-actually-caching-and-not-just-pretending/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 17:58:00 +0000</pubDate>
		<dc:creator>steve</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Phusion Passenger]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[passenger]]></category>

		<guid isPermaLink="false">http://www.alfajango.com/blog/?p=177</guid>
		<description><![CDATA[We recently worked on a Rails application that had page and action caching set up, only to find that it was not actually working. It occurred to me that many Rails/Passenger/Apache applications may have caching set up in a way that it appears to be caching, when it is not actually caching.]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.alfajango.com/blog/wp-content/uploads/2010/03/rails-cache-broken.jpg"><img class="alignright size-full wp-image-181" title="rails-cache-broken" src="http://www.alfajango.com/blog/wp-content/uploads/2010/03/rails-cache-broken.jpg" alt="" width="300" height="312" /></a>We recently worked on a Rails application that had page and action caching set up, only to find that it was not actually working. It occurred to me that many Rails/Passenger/Apache applications may have caching set up in a way that it appears to be caching, when it is not actually caching. Searching through the interwebs for various Passenger/Apache configurations, such as <a href="http://wiki.github.com/browsermedia/browsercms/page-caching-with-phusion-passenger">this snippet on Github</a> or <a href="http://groups.google.com/group/phusion-passenger/browse_thread/thread/dde48ec055f6519f">this discussion on Google Groups</a>, I found that many did not work with the most recent version of <a href="http://modrails.com/">Phusion Passenger</a>. What&#8217;s more, these configurations give the appearance that they <em>are</em> working.
</p><p>
For an introduction to caching with Rails, check out this post: <a href="http://guides.rubyonrails.org/caching_with_rails.html">Caching With Rails: An Overview</a>
</p>
<h2>The Appearance That Caching Works</h2>
<p>
Here is what I mean by giving the appearance that caching is working:
</p><p>
<ol>
	<li>you add <code class="codecolorer rails default"><span class="rails">caches_page <span style="color:#ff3333; font-weight:bold;">:index</span></span></code> to your controller, for example</li>
	<li>configure and restart your server</li>
	<li>load the index page</li>
	<li>check your rails cache directory on your server, and see the index.html</li>
	<li>it works! (hint: this step is wrong, you&#8217;re not quite there yet)</li>
</ol>
</p><p>
Just because Rails is generating your cached page does not mean that your server is subsequently serving the cached <code class="codecolorer text default"><span class="text">.html</span></code> file instead of sending the request to Rails again.
</p>
<h2>Ensure It&#8217;s Serving the Cached Page</h2>
<p>
To make sure it&#8217;s subsequently serving the cached page, the easiest method is to look at the created date on the cached file on your server, for instance:
</p>
<span id="more-177"></span>
<p>

<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> myapp<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>cache</div></div>

</p><p>
You will get an output similar to this:
</p><p>

<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-rw-r--r-- 1 deploy deploy 10930 2010-03-09 17:49 index.html</div></div>

</p><p>
Now wait a minute, and then reload the page in your browser. Once again type <code class="codecolorer bash default"><span class="bash"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> myapp<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>cache</span></code>, and make sure that the file still shows the same created date/time. If it instead shows this:
</p><p>

<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-rw-r--r-- 1 deploy deploy 10930 2010-03-09 17:50 index.html</div></div>

</p><p>
&#8230;then you have a problem. Your server is sending every request back to Rails, where Rails is then re-generating the cached page.
</p><p>
<em><strong>Alternative 1</strong>: You can also use Apache&#8217;s <code class="codecolorer apache default"><span class="apache"><span style="color: #00007f;">RewriteLog</span></span></code> to watch what is being served. Just add this to your site&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file and then <code class="codecolorer bash default"><span class="bash"><span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-f</span></span></code> the rewrite_log:</em>
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;<br />
&nbsp; ...<br />
&nbsp; <span style="color: #adadad; font-style: italic;"># Comment out to disable rewrite debugging</span><br />
&nbsp; <span style="color: #00007f;">RewriteLog</span> /path/to/myapp/current/log/rewrite_log<br />
&nbsp; <span style="color: #00007f;">RewriteLogLevel</span> 9<br />
&nbsp; ...<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p><p>
<em><strong>Alternative 2:</strong> You can also use the <code class="codecolorer bash default"><span class="bash">top</span></code> command to watch your running processes and make sure the rails process doesn&#8217;t rise to the top while reloading your page. This is obviously not very scientific, though, and won&#8217;t work if you have any external traffic on the site.</em>
<h2>The Solution: Proper Server Configuration</h2>
The problem here is that your server is not properly configured to intercept calls to cached pages. This could take a bit of Googling and debugging, but the important thing here is that we&#8217;ve identified the problem and how to tell when it&#8217;s fixed. For completion&#8217;s sake, I&#8217;ll go ahead and show the proper configuration we&#8217;ve determined for Passenger 2.2.8:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; ...<br />
&nbsp; RailsAllowModRewrite <span style="color: #0000ff;">on</span><br />
&nbsp; <span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span><br />
&nbsp; <span style="color: #adadad; font-style: italic;">#apache should serve cached pages</span><br />
&nbsp; <span style="color: #00007f;">RewriteRule</span> ^/$ /cache/index.html [QSA]<br />
&nbsp; <span style="color: #00007f;">RewriteRule</span> ^([^.]+)$ /cache/$1.html [QSA]<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p>
<div class="in-depth">
UPDATE: Christoffer Sawicki has <a href="http://termos.vemod.net/page-caching-with-rails-and-passenger">a set of rewrite rules</a> that I like much better than what I originally posted here. These rules explicitly check to make sure the cached file exists and that the current request is a GET request (instead of a POST, PUT, or DELETE), without relying on Rails magic. In short, use these instead:
<p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; ...<br />
&nbsp; RailsAllowModRewrite <span style="color: #0000ff;">On</span> &nbsp;<br />
&nbsp; <span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span><br />
<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{THE_REQUEST} ^(GET|HEAD)<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{REQUEST_URI} ^/([^.]+)$<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{DOCUMENT_ROOT}/cache/%1.html -f<br />
&nbsp; <span style="color: #00007f;">RewriteRule</span> ^/[^.]+$ /cache/%1.html [QSA,L]<br />
<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{THE_REQUEST} ^(GET|HEAD)<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{DOCUMENT_ROOT}/cache/index.html -f<br />
&nbsp; <span style="color: #00007f;">RewriteRule</span> ^/$ /cache/index.html [QSA,L]<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p>
</div>
<p>
Happy caching! For more tips for getting the best performance out of your server, check out <a href="http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/">Performance Tuning for Phusion Passenger (An Introduction)</a>, and <a href="http://www.alfajango.com/blog/how-to-monitor-your-railspassenger-app-with-munin/">How to Monitor Your Rails/Passenger App with Munin</a>.
</p>
<h2><em>Update: Proper Nginx Configuration</em></h2>
<p>
For extra completeness, thank you to <em>atambo</em> on <a href="http://news.ycombinator.com/item?id=1185473">Hacker News</a> for pointing me to <a href="http://brainspl.at/nginx.conf.txt">this proper configuration for Rails caching on Nginx</a>. You can read the comments for an explanation.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.alfajango.com/blog/make-sure-your-rails-application-is-actually-caching-and-not-just-pretending/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance Tuning for Phusion Passenger (an Introduction)</title>
		<link>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/</link>
		<comments>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 13:54:07 +0000</pubDate>
		<dc:creator>steve</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[performance tuning]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[swap]]></category>

		<guid isPermaLink="false">http://www.alfajango.com/blog/?p=142</guid>
		<description><![CDATA[Once you've launched your application and people start actually using it, you may find server experiencing excessive swapping once the traffic begins to pick up. Before getting too deep into scaling, caching, upgrading your server, etc., there are a few Passenger settings you can tune to wring out the best performance your existing stack. I'll address these settings with a few rules of thumb we've gathered from the Passenger documentation and simple trial-and-error.]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.alfajango.com/blog/wp-content/uploads/2010/03/Picture-10.png"><img class="size-medium wp-image-151 alignright" title="Picture 10" src="http://www.alfajango.com/blog/wp-content/uploads/2010/03/Picture-10-237x300.png" alt="" width="237" height="300" /></a><a href="http://www.modrails.com/">Phusion Passenger</a> (aka mod_rails) allows for easy and scalable deployment of Ruby on Rails applications on Apache or Nginx servers. Part of what makes it so easy is that it comes with suitable default settings right out of the box, so that you don&#8217;t need to concern yourself with any of the details when deploying your application to production.
</p><p>
However, once you&#8217;ve launched your application and people start actually using it, you may find server experiencing excessive swapping once the traffic begins to pick up. Before getting too deep into scaling, caching, upgrading your server, etc., there are a few Passenger settings you can tune to wring out the best performance your existing stack. I&#8217;ll address these settings with a few rules of thumb we&#8217;ve gathered from the <a href="http://www.modrails.com/documentation.html">Passenger documentation</a> and simple trial-and-error.
</p><p>
Before getting started, we highly recommend employing Munin (<a href="http://www.alfajango.com/blog/how-to-monitor-your-railspassenger-app-with-munin/">instructions for Munin with Passenger here</a>) and NewRelic Performance Monitoring for your Passenger/Rails application, so that you can fine-tune your settings for the best performance.
</p><p>
<em>Note: the following settings are specific for Apache, you will need to modify the syntax/file-names for Nginx.</em>
</p>
<h2>Per-server Settings</h2>
<p>
<span style="font-weight: normal;">These settings are set per-server, meaning if you have multiple applications running on the server, these should be set only once throughout all of your <code class="codecolorer text default"><span class="text">.conf</span></code> files. If you have only one Rails/Passenger app on the server, then you may set these in that application&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file. On Ubuntu, this may be found at <code class="codecolorer text default"><span class="text">/etc/apache2/sites-available/yourapp.conf</span></code>. </span>
</p>
<span id="more-142"></span>
<p>
A more appropriate place for these however would be in Apache&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file.
</p>
<h3>PassengerMaxPoolSize and PassengerPoolIdleTime</h3>
<p>
<code class="codecolorer text default"><span class="text">/etc/apache2/apache2.conf</span></code>:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># PassengerMaxPoolSize</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 6</span><br />
<span style="color: #adadad; font-style: italic;"># For 2gb RAM: 30</span><br />
<span style="color: #adadad; font-style: italic;"># For 256 slice with MySQL running: 2</span><br />
PassengerMaxPoolSize <span style="color: #ff0000;">2</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerPoolIdleTime</span><br />
<span style="color: #adadad; font-style: italic;"># Recommended to be avg time per page * 2</span><br />
<span style="color: #adadad; font-style: italic;"># In Google Analytics... (Avg time on site / Avg page views) * 2</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 300</span><br />
PassengerPoolIdleTime <span style="color: #ff0000;">150</span></div></div>

</p><p>
You can essentially copy and paste this into your apache conf file. I&#8217;ve added some notes in there to help you remember how they should be tuned.</p>
<p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_passengermaxpoolsize_lt_integer_gt"><code class="codecolorer apache default"><span class="apache">PassengerMaxPoolSize</span></code></a>: This is the number of Passenger instances that can be spawned at once for your site. This basically relates directly to the amount of RAM that is available to your application. However, it is not a linear relationship. It&#8217;s more of an exponential relationship. Numbers that we and others have found to work well are:</p>
<table>
<tbody>
<tr>
<th>RAM (MB)</th>
<th>PassengerMaxPoolSize</th>
</tr>
<tr>
<td>256</td>
<td>2</td>
</tr>
<tr>
<td>512</td>
<td>6 (default)</td>
</tr>
<tr>
<td>1024 (1GB)</td>
<td>15</td>
</tr>
<tr>
<td>2048 (2GB)</td>
<td>30</td>
</tr>
</tbody>
</table>
<p>
Since this depends on available RAM, this will vary depending on what other processes are running on your server, such as MySQL, etc. If you have Munin installed with the Passenger plugins, you can monitor the Passenger Memory Stats and Passenger Status graphs to tweak this as necessary.
</p><p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#PassengerPoolIdleTime"><code class="codecolorer apache default"><span class="apache">PassengerPoolIdleTime</span></code></a>: This is essentially the amount of time (in seconds) that each Passenger instance continues to run after it becomes idle (i.e. someone stops clicking around the site). According to the documentation, this should be set to the average time a visitor views a page multiplied by 2. This of course prevents Passenger instances from shutting down (and thus having to re-spawn) while someone is viewing a page.
</p><p>
If your application has been in production long enough to have a few hundred viewers and you have Google Analytics installed, an easy way to get the ideal setting for your application is to take the Average Time on Site, divide by the Average Page Views per Visit, and multiply by 2.
</p><p>

<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">( Average Time on Site / Average Page Views per Visit ) x 2</div></div>

</p>
<h3>PassengerMaxInstancesPerApp (optional)</h3>
<p>
If you do have more than one Rails/Passenger application on the server, you may optionally set <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code>.
</p><p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_passengermaxinstancesperapp_lt_integer_gt"><code class="codecolorer apache default"><span class="apache">PassengerMaxInstancesPerApp</span></code></a>: This is the maximum number of instances each application may use of the total <code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code>. Therefore, <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code> must be smaller than <code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code>.
</p><p>
For example, if you have 2 applications running on one 256MB server, you may want to set <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code> to 1, so that one app may never hog all of the rails instances at the expense of the other app. If you have 2 applications running on a 512MB server (<code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code> 6), then you may want to set cci]PassengerMaxInstancesPerApp[/cci] to 4 or 5 to allow each app to better handle higher load (hopefully while the other app is under less load).
</p>
<h3>RailsAutoDetect</h3>
<p>
To get a little more performance out of your server, you can also turn the <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_railsautodetect_lt_on_off_gt"><code class="codecolorer apache default"><span class="apache">RailsAutoDetect</span></code></a> setting of Passenger off. This will make it so that each request through Apache does not check to see if it is a Rails request that requires Passenger. However, this will now require that you explicitly tell Apache which requests are Rails and do require Passenger, using <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#RailsBaseURI"><code class="codecolorer apache default"><span class="apache">RailsBaseURI</span></code></a>. This is easy though.
</p><p>
Again, in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># Must be used with RailsBaseURI in Vhost</span><br />
RailsAutoDetect <span style="color: #0000ff;">off</span></div></div>

</p><p>
Now, within your the Vhost block in your site&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file, you will need to set the <code class="codecolorer text default"><span class="text">RailsBaseURI</span></code>:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; ...<br />
&nbsp; RailsBaseURI /<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p><p>
Here is a complete example of the Passenger block in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># PassengerMaxPoolSize</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 6</span><br />
<span style="color: #adadad; font-style: italic;"># For 2gb RAM: 30</span><br />
<span style="color: #adadad; font-style: italic;"># For 256 slice with MySQL running: 2</span><br />
PassengerMaxPoolSize <span style="color: #ff0000;">2</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerPoolIdleTime</span><br />
<span style="color: #adadad; font-style: italic;"># Recommended to be avg time per page * 2</span><br />
<span style="color: #adadad; font-style: italic;"># In Google Analytics... (Avg time on site / Avg page views) * 2</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 300</span><br />
PassengerPoolIdleTime <span style="color: #ff0000;">144</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerMaxInstancesPerApp &lt; PassengerMaxPoolSize </span><br />
PassengerMaxInstancesPerApp <span style="color: #ff0000;">1</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># Must be used with RailsBaseURI in Vhost</span><br />
RailsAutoDetect <span style="color: #0000ff;">off</span></div></div>

</p></p><p>
And a simple <code class="codecolorer text default"><span class="text">sites-available/yourapp.conf</span></code> may look like this:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; <span style="color: #00007f;">ServerName</span> www.yourapp.com<br />
&nbsp; <span style="color: #00007f;">DocumentRoot</span> /home/deploy/yourapp/current/public<br />
&nbsp; &lt;<span style="color: #000000; font-weight:bold;">Directory</span> <span style="color: #7f007f;">&quot;/home/deploy/yourapp/current/public&quot;</span>&gt;<br />
&nbsp; &nbsp; <span style="color: #00007f;">Options</span> <span style="color: #0000ff;">FollowSymLinks</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">AllowOverride</span> <span style="color: #0000ff;">None</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">Order</span> <span style="color: #00007f;">allow</span>,<span style="color: #00007f;">deny</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">Allow</span> from <span style="color: #0000ff;">all</span><br />
&nbsp; &lt;/<span style="color: #000000; font-weight:bold;">Directory</span>&gt;<br />
&nbsp; RailsBaseURI /<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p>
<h2>More Passenger Configuration Settings</h2>
<p>
These settings so far only scratch the surface of Passenger configuration. Give the <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_configuring_phusion_passenger">Passenger documentation</a> a good read-through for more information on configuring Passenger.
</p>
<h2>Further Reading: Apache Settings</h2>
<p>
At this point, if you still need to squeeze more performance out of your server (and you will eventually), you&#8217;ll want to configure Apache to utilize its resources most efficiently with Passenger. I recommend reading through <a href="http://forum.slicehost.com/comments.php?DiscussionID=1776">this thread on the Slicehost forums</a> to get a good idea just how much trial-and-error this will take. <strong>Keep a close eye on your server when you tweak these settings, you may end up making it slower several times before finding that sweet spot.</strong>
</p><p>
You may read the <a href="http://httpd.apache.org/docs/1.3/mod/core.html">documentation for each setting in the Apache docs</a> (note that these are the Apache 1.3 docs, but they still explain purpose of each setting well). For more information using these settings to reduce the RAM consumed by Apache, see <a href="http://forum.slicehost.com/comments.php?DiscussionID=3313">this other Slicehost forum</a> and <a href="http://programming-gone-awry.blogspot.com/2009/06/how-to-save-much-ram-when-running-rails.html">this blog post on saving RAM with Rails and Passenger</a>.
</p><p>
If you&#8217;re impatient and just want to get something better than default, here are some numbers that worked well for us on a recent application running on a 256MB VPS on Slicehost. These settings can be found in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file.
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:300px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># prefork MPM</span><br />
<span style="color: #adadad; font-style: italic;"># StartServers: number of server processes to start</span><br />
<span style="color: #adadad; font-style: italic;"># MinSpareServers: minimum number of server processes which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxSpareServers: maximum number of server processes which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxClients: maximum number of server processes allowed to start</span><br />
<span style="color: #adadad; font-style: italic;"># MaxRequestsPerChild: maximum number of requests a server process serves</span><br />
&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mpm_prefork_module&gt;<br />
&nbsp; <span style="color: #00007f;">StartServers</span>          2<br />
&nbsp; <span style="color: #00007f;">MinSpareServers</span>       1<br />
&nbsp; <span style="color: #00007f;">MaxSpareServers</span>       5<br />
&nbsp; <span style="color: #00007f;">MaxClients</span>           10<br />
&nbsp; <span style="color: #00007f;">MaxRequestsPerChild</span>   100<br />
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;<br />
<span style="color: #adadad; font-style: italic;"># worker MPM</span><br />
<span style="color: #adadad; font-style: italic;"># StartServers: initial number of server processes to start</span><br />
<span style="color: #adadad; font-style: italic;"># MaxClients: maximum number of simultaneous client connections</span><br />
<span style="color: #adadad; font-style: italic;"># MinSpareThreads: minimum number of worker threads which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxSpareThreads: maximum number of worker threads which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># ThreadsPerChild: constant number of worker threads in each server process</span><br />
<span style="color: #adadad; font-style: italic;"># MaxRequestsPerChild: maximum number of requests a server process serves</span><br />
&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mpm_worker_module&gt;<br />
&nbsp; <span style="color: #00007f;">StartServers</span>          2<br />
&nbsp; <span style="color: #00007f;">MaxClients</span>           10<br />
&nbsp; <span style="color: #00007f;">MinSpareThreads</span>      25<br />
&nbsp; <span style="color: #00007f;">MaxSpareThreads</span>      75<br />
&nbsp; <span style="color: #00007f;">ThreadsPerChild</span>      25<br />
&nbsp; <span style="color: #00007f;">MaxRequestsPerChild</span>   100<br />
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;</div></div>

</p>]]></content:encoded>
			<wfw:commentRss>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
