<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>The Vose Way: Tag mod_proxy_balancer</title>
    <link>http://www.chuckvose.com/articles/tag/mod_proxy_balancer</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A Blog about Butter, Cheese, and Ruby on Rails</description>
    <item>
      <title>RailsMachine / Slingshot image loader slowdown</title>
      <description>&lt;h2&gt;Background / Analysis&lt;/h2&gt;


	&lt;p&gt;Recently a fellow asked me to help him diagnose a slowdown in his rails stack at the Ruby Brigade meeting in Seattle.&lt;/p&gt;


	&lt;p&gt;After going through the normal procedures of checking mysql indexes, talking about caching, and checking the code for bizarreness I still couldn&amp;#8217;t find anything really out of the ordinary.&lt;/p&gt;


	&lt;p&gt;Finally we turned to &lt;a href="http://getfirebug.com"&gt;Firebug&lt;/a&gt; and found that the images were taking an eternity to download. This had happened to On &amp;#38; On Creative earlier on this year so the solution was quick at hand.&lt;/p&gt;


	&lt;h2&gt;Solution&lt;/h2&gt;


	&lt;p&gt;RailsMachine and the mongrel docs themselves recommend the following code for apache:&lt;/p&gt;


&lt;code&gt;
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://NEF_mongrel_cluster%{REQUEST_URI} [P,QSA,L]
&lt;/code&gt;

	&lt;p&gt;But there&amp;#8217;s a hidden gotcha that I still haven&amp;#8217;t fully figured out. Sometimes, usually when images aren&amp;#8217;t in the top level of /images, apache will not match the RewriteCond and pass the image to mongrel instead of loading it itself.&lt;/p&gt;


Mongrel is terrible at serving images and static content from the filesystem. In the words of Zed Shaw, &amp;#8220;This means mongrel will serve images, javascript, files, and everything else. It&#8217;s quite fast at this, but Apache can do it better.&amp;#8221; 
	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; http://mongrel.rubyforge.org/docs/apache.html&lt;/p&gt;


	&lt;p&gt;So if you see a slowdown on images, you run mongrel through apache&amp;#8217;s mod_proxy_balancer, and you used the config from the mongrel website or from RailsMachine/Slingshot&amp;#8217;s capistrano script, try this code and see if it helps:&lt;/p&gt;


&lt;code&gt;
# Redirect all non-static requests to cluster
RewriteCond %{REQUEST_FILENAME} !.*(jpg|gif|png|js|css)$
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://NEF_mongrel_cluster%{REQUEST_URI} [P,QSA,L]
&lt;/code&gt;

	&lt;h2&gt;Did It Work?&lt;/h2&gt;


	&lt;h3&gt;Checking the rewrite logs&lt;/h3&gt;


	&lt;p&gt;My computer is exceedingly slow so I can&amp;#8217;t rely on my browser to tell me how fast an image is really downloading. If you&amp;#8217;re like me it might be worth watching the rewrite logs to see if the images are being passed through rather than being rendered.&lt;/p&gt;


	&lt;p&gt;Drop this into your virtual host configuration right after the RewriteEngine On:&lt;/p&gt;


&lt;code&gt;
RewriteLog logs/myapp_rewrite_log
RewriteLogLevel 9
&lt;/code&gt;

	&lt;p&gt;Then check the apache logs folder (often /var/log/httpd or /usr/local/apache2/logs) for the myapp_rewrite_log. After loading a page it should look something like this:&lt;/p&gt;


&lt;code&gt;
init rewrite engine with requested uri /images/drivers_panel.jpg
applying pattern '^/(.*)$' to uri '/images/drivers_panel.jpg'
RewriteCond: input='/var/www/apps/NEF/current/public//images/drivers_panel.jpg' pattern='!.*(jpg|gif|png|js|css)' =&amp;gt; not-matched
pass through /images/drivers_panel.jpg
&lt;/code&gt;

	&lt;p&gt;Rather than this contrived example:&lt;/p&gt;


&lt;code&gt;
init rewrite engine with requested uri /images/extra/dirs/my_image.gif
applying pattern '^/(.*)$' to uri '/images/extra/dirs/my_image.gif'
RewriteCond: input='/var/www/apps/myapp/current/public/images/extra/dirs/my_image.gif' pattern='!-f' =&amp;gt; matched
rewrite '/images/extra/dirs/my_image.gif' -&amp;gt; 'balancer://myapp_mongrel_cluster/'
forcing proxy-throughput with balancer://myapp_mongrel_cluster/
go-ahead with proxy request proxy:balancer://myapp_mongrel_cluster/ [OK]
&lt;/code&gt;

	&lt;h3&gt;Checking the rails logs&lt;/h3&gt;


	&lt;p&gt;The rewrite logs are cool and handy so I put them first but you should also be seeing entries in your development.log or production.log.
Try the following snippet, you may be surprised to see the results:&lt;/p&gt;


&lt;code&gt;
cat development.log production.log | egrep '(jpg|png|gif|js|css)' | less
&lt;/code&gt;</description>
      <pubDate>Sun, 22 Apr 2007 19:56:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:507d5591-974f-4f65-a756-054a31476cec</guid>
      <author>vosechu@create-on.com (Chuck Vose)</author>
      <link>http://www.chuckvose.com/articles/2007/04/22/railsmachine-slingshot-image-loader-slowdown</link>
      <category>Rails</category>
      <category>Rails</category>
      <category>Apache</category>
      <category>mod_rewrite</category>
      <category>mod_proxy_balancer</category>
      <category>proxy</category>
      <category>image</category>
      <category>mongrel</category>
      <category>slow</category>
      <category>firebug</category>
      <category>Rails</category>
    </item>
  </channel>
</rss>
