title: Using Nginx to serve static assets from remote rails upstream server
body: <p>I'm using Nginx as an http load balancer for multiple upstream backend Rails servers using remote TCP connections. They're all hosting the same application and use the same static precompiled assets sitting under the <code>public/assets</code> directory.</p>
  
  <p>Now I'm running into a problem serving the static assets for these remote connections using the suggested nginx configuration provided on the Rails asset pipeline documentation page <a href="http://guides.rubyonrails.org/asset_pipeline.html#in-production" rel="nofollow">here</a>.  </p>
  
  <pre><code>upstream backend {
          ip_hash;
          server upstream1.example.com:8080;
  }
  
  server {
          client_max_body_size 100M;
          listen 80;
          server_name "example.com";
  
          location / {
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header X-Real-IP  $remote_addr;
                  proxy_set_header Host $http_host;
                  proxy_redirect off;
  
                  if (!-f $request_filename) {
                          proxy_pass http://backend;
                          break;
                  }
          }
          location ~ ^/assets/ {
           expires 1y;
           add_header Cache-Control public;
  
           add_header ETag "";
           break;
         }
  
  }
  </code></pre>
  
  <p>With the above configurations set, navigating to an asset manaully in the browser, e.g, `example.com/assets/image-1ae4dda6436928deeb4bc903fd421572.jpg" seems to cause Nginx to try and load the file from the load balancing server instead of the remote servers, here's the output from my error log:</p>
  
  <blockquote>
    <p>2014/06/17 12:55:27 [error] 5953#0: *1 open()
    "/etc/nginx/html/assets/image-1ae4dda6436928deeb4bc903fd421572.jpg"
    failed (2: No such file or directory), client: 205.200.252.55, server:
    example.com, request: "GET
    /assets/image-1ae4dda6436928deeb4bc903fd421572.jpg HTTP/1.1", host:
    "example.com"</p>
  </blockquote>
  
  <p>Any workarounds? I'm planning on using cloudfront to serve all of my static assets, but I'm pretty sure I need a central server to serve all the files from initially in order to get it working and avoid issues with cross-domain fonts on browsers like Firefox and IE.</p>