title: Rails 3 Subdomain with Nginx and Unicorn
body: <p>I have a rails app which is deployed over Capistrano to a VPS with a very similar setup to <a href="http://railscasts.com/episodes/335-deploying-to-a-vps" rel="nofollow">this Railscast</a>. I have both mydomain.co.uk and admin.mydomain.co.uk. The subdomains work fine locally using lvh.me and the standard Webbrick server, but in production admin.mydomain.co.uk shows exactly the same content as mydomain.co.uk.</p>
  
  <p><strong>My routes.rb file:</strong></p>
  
  <pre><code>class AdminDomain
    def self.matches?(request)
      puts "Sub = #{request.subdomain}"
      request.subdomain.present? &amp;&amp; request.subdomain == "admin"
    end
  end
  
  MyApp::Application.routes.draw do
  
    constraints(AdminDomain) do
      scope :module =&gt; "admin" do
        match '', to: 'admin#index'
  
        resources :users
      end
    end
  
    # All the mydomain.co.uk routes...
  </code></pre>
  
  <p><strong>My Nginx config:</strong></p>
  
  <pre><code>upstream unicorn {
    server unix:/tmp/unicorn.&lt;%= application %&gt;.sock fail_timeout=0;
  }
  
  server {
    listen 80;
    root &lt;%= current_path %&gt;/public;
  
    server_name mydomain.co.uk admin.mydomain.co.uk;
  
    listen 443 ssl;
    ssl_certificate /home/deployer/mydomain_combined.crt;
    ssl_certificate_key /home/deployer/mydomain.key;
    proxy_set_header X-Forwarded-Proto $scheme;
  
    auth_basic            "Restricted";
    auth_basic_user_file  htpasswd;
  
    location ^~ /assets/ {
      gzip_static on;
      expires max;
      add_header Cache-Control public;
    }
  
    location = /favicon.ico {
      expires    max;
      add_header Cache-Control public;
    }
  
    if (-f $document_root/system/maintenance.html) {
      return 503;
    }
  
    error_page 503 @maintenance;
  
    location @maintenance {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }
  
    try_files $uri/index.html $uri @unicorn;
    location @unicorn {
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://unicorn;
    }
  
    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
  }
  </code></pre>
  
  <p>The only idea I have is that Nginx is not passing on the request url to the unicorn. I had a similar issue with SSL but solved that by adding <code>proxy_set_header X-Forwarded-Proto $scheme;</code>. How can I get the subdomains to function correctly in a production environment under Nginx and Unicorn?</p>