2
votes

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 public/assets directory.

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 here.

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;
       }

}

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:

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"

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.

1
In your assets location you have nothing about upstream.Alexey Ten
@AlexeyTen How would I go about adding that in? Something like root backend; maybe?Noz

1 Answers

0
votes

I assume that each of you app instances (upstream) also use nginx as a proxy.

You should put this in the nginx responsible for each of you app instances (upstream) not in the nginx config for the load balancer

location ~ ^/assets/ {
     expires 1y;
     add_header Cache-Control public;

     add_header ETag "";
     break;
   }