<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/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>Robby on Rails: Goodbye Pound, Hello Nginx</title>
    <link>http://www.robbyonrails.com/articles/2007/02/01/goodbye-pound-hello-nginx</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Goodbye Pound, Hello Nginx</title>
      <description>&lt;p&gt;I&amp;#8217;ve been using pound for several months and it&amp;#8217;s been a good relationship. Except, for some strange reason, I noticed that I was getting development mode errors when it was running in production mode. I thought there might be an issue with my mongrel cluster&amp;#8230; but that wasn&amp;#8217;t the case.&lt;/p&gt;


	&lt;p&gt;Let me give you a little background to how we&amp;#8217;re encouraging customers to handle their deployment on &lt;a href="http://www.planetargon.com"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Most of our hosting customers&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt; have three tiers (unless you have your own static IP address), one which we manage, two that you manage.&lt;/p&gt;


	&lt;p&gt;We handle the main web server/proxy server and proxy to your desired load balancer/proxy/server, which is generally any of the following options&amp;#8230; depending on your preference.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://www.apsis.ch/pound"&gt;Pound&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://lighttpd.org"&gt;Lighttpd&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://nginx.net/"&gt;Nginx&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.apache.org"&gt;Apache&lt;/a&gt; 2.x or Apache 1.3.x&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://mongrel.rubyforge.org"&gt;Mongrel&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Each customer has a unique proxy server port and a range of other ports for their mongrel clusters.&lt;/p&gt;


	&lt;p&gt;So&amp;#8230; the typical setup is&amp;#8230;&lt;/p&gt;


	&lt;p&gt;Apache(external:80) [proxies to]==&amp;gt; Pound(localhost:8050) [proxies to]==&amp;gt; Mongrel::Cluster(localhost:10500-10503)&lt;/p&gt;


	&lt;p&gt;Well, when a request comes in through Apache, it gets passed off to Pound and each tier has it&amp;#8217;s own headers. By the time that it reaches Mongrel, all the requests appear to be coming &lt;em&gt;from&lt;/em&gt; &lt;code&gt;localhost&lt;/code&gt;.. not the remote address of the person using your application. Notice nothing but &lt;code&gt;localhost&lt;/code&gt; requests in your production.log? ...this is the reason.&lt;/p&gt;


	&lt;p&gt;So, what side-effects does this have? Well, aside from every request looking local&amp;#8230; Rails will, by default, output a normal development-mode error message if the request is coming from &lt;code&gt;localhost&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
    # found in...
    # actionpack/lib/action_controller/rescure.rb

    # Exception handler called when the performance of an action raises an exception.
    def rescue_action(exception)
      log_error(exception) if logger
      erase_results if performed?

      if consider_all_requests_local || local_request?
        rescue_action_locally(exception)
      else
        rescue_action_in_public(exception)
      end
    end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;It seems that this currently causes the &lt;a href="http://dev.rubyonrails.org/svn/rails/plugins/exception_notification/"&gt;exception notification&lt;/a&gt; plugin, which we often use, to not work. We noticed this in a staging environment for an application that we&amp;#8217;re building for a client about a month ago. After debugging &lt;span class="caps"&gt;SMTP&lt;/span&gt; servers, mongrel configuration&amp;#8230; I was baffled.&lt;/p&gt;


	&lt;h2&gt;Nginx to the &lt;code&gt;rescue&lt;/code&gt;&lt;/h2&gt;


	&lt;p&gt;After some investigation and attempts to find a workaround in Pound, I decided to redeploy my blog with Nginx. This was a pretty painless process and I was able to use &lt;a href="http://docs.planetargon.com/wiki/show/Setup+Nginx"&gt;the example&lt;/a&gt; posted on the &lt;a href="http://docs.planetargon.com/"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt; Documentation Project&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Nginx allows you to do the following to overwrite the headers being passed to Mongrel.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;Problem Solved!&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;If there is a workaround for this in Pound, I&amp;#8217;d love to be able to relay this information to our customers that haven&amp;#8217;t made the switch yet.&lt;/p&gt;


	&lt;p&gt;Thank you, &lt;a href="http://blog.brightredglow.com"&gt;Brian&lt;/a&gt; and &lt;a href="http://workingwithrails.com/person/7552"&gt;Timothy&lt;/a&gt; for encouraging me to finally switch my blog to Nginx. ;-)&lt;/p&gt;


	&lt;p&gt;If you have questions related to deploying Rails applications, be sure to check out the &lt;a href="http://groups.google.com/group/rubyonrails-deployment/about"&gt;Rails Deployment google group&lt;/a&gt;.&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; For more information about our hosting, visit &lt;a href="http://www.planetargon.com/hosting.html"&gt;http://planetargon.com/hosting.html&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Thu, 01 Feb 2007 13:48:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:14935b91-ffe5-4379-8ab9-42596c86ee78</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/02/01/goodbye-pound-hello-nginx</link>
      <category>Ruby on Rails</category>
      <category>PLANET ARGON</category>
      <category>hosting</category>
      <category>nginx</category>
      <category>pound</category>
      <category>deployment</category>
    </item>
  </channel>
</rss>
