title: How do I pass the HTTP username from Apache to Mongrel/Rails?
body: <p><strong>The goal:</strong> running a Rails application on Mongrels, allowing access through Apache after doing basic HTTP Authentication</p>
  
  <p><strong>The problem:</strong> reading the supplied username from within Rails</p>
  
  <p>Apache:</p>
  
  <pre><code>&lt;Proxy balancer://mongrel_cluster&gt;
      BalancerMember http://127.0.0.1:4001
      # ...
  
      Order deny,allow
      Deny from all
  
      AuthType Basic
      AuthName "&lt;realm&gt;"
      AuthUserFile "&lt;users-file&gt;"
      AuthGroupFile "&lt;groups-file&gt;"
      Require group &lt;group&gt;
  
      Satisfy Any
  &lt;/Proxy&gt;
  
  RewriteEngine On
  # ...
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
  </code></pre>
  
  <p>That works just fine, Apache forces the user to authenticate and forwards to Rails if successful. I omitted a few lines for handling static files and such, and triggering authentication for them as well.</p>
  
  <p>The environment variables from Rails' perspective contain the usual entries and additionally <code>HTTP_X_FORWARDED_HOST</code>, <code>HTTP_X_FORWARDED_SERVER</code> and <code>HTTP_X_FORWARDED_FOR</code>. I was unable to pass custom environment variables by adding them to the rewrite rule:</p>
  
  <pre><code>RewriteRule ... [P,QSA,L,E=foo:bar]
  </code></pre>
  
  <p>Any thoughts?</p>