使用Nginx从远程rails上游服务器提供静态资产

时间:2022-09-05 20:38:24

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.

我正在使用Nginx作为使用远程TCP连接的多个上游后端Rails服务器的http负载均衡器。它们都托管相同的应用程序,并使用位于public / assets目录下的相同静态预编译资产。

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.

现在,我遇到了使用Rails资产管道文档页面上提供的建议的nginx配置为这些远程连接提供静态资产的问题。

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:

通过设置上述配置,在浏览器中手动导航到资产,例如“example.com/assets/image-1ae4dda6436928deeb4bc903fd421572.jpg”似乎会导致Nginx尝试从负载平衡服务器而不是远程服务器加载文件,这是我的错误日志的输出:

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"

2014/06/17 12:55:27 [错误] 5953#0:* 1打开()“/etc/nginx/html/assets/image-1ae4dda6436928deeb4bc903fd421572.jpg”失败(2:没有这样的文件或目录),客户端:205.200.252.55,server:example.com,request:“GET /assets/image-1ae4dda6436928deeb4bc903fd421572.jpg HTTP / 1.1”,主持人:“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.

有任何变通方法吗?我打算使用cloudfront来提供我所有的静态资产,但我很确定我需要一个*服务器来提供最初的所有文件,以使其正常工作并避免在浏览器上出现跨域字体问题Firefox和IE浏览器。

1 个解决方案

#1


0  

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

我假设每个app实例(上游)也使用nginx作为代理。

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

你应该把这个放在负责每个app实例(上游)的nginx中,而不是负载均衡器的nginx配置中

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

     add_header ETag "";
     break;
   }

#1


0  

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

我假设每个app实例(上游)也使用nginx作为代理。

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

你应该把这个放在负责每个app实例(上游)的nginx中,而不是负载均衡器的nginx配置中

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

     add_header ETag "";
     break;
   }