如何使用NGINX反向代理将Dragonfly图像保存到远程服务器?

时间:2015-06-25 19:50:50

标签: ruby-on-rails ruby nginx passenger dragonfly-gem

问题:

图像无法读取/写入Dragonfly的数据库服务器文件结构。我可以通过所有Ruby模型的活动记录与我的数据库进行交互。我所有的静态资产都在运作。用户生成的图像应保存为www.test.example.com/media/AgGdsgDGsdgsDGSGdsgsdg ... 在我的远程服务器上。然而,他们将被保存在他们上传的任何应用服务器上。

背景:

Ruby / Rails,Nginx,Passenger。我们正在从单一服务器解决方案转向3服务器解决方案。我有两个位于数据库服务器后面的应用服务器。我正在使用Dragonfly Gem来生成用户生成的图像和其他内容。在我们当前的单服务器设置中,一切都只指向localhost并且运行良好。

10.102.66.4是我的数据库服务器的局域网IP。

APP SERVERS NGINX.CONF:

user  pete;
...
http {
  passenger_pre_start http://example.com;
  passenger_pre_start http://example.com:3000;
  ...
  proxy_cache_path /home/pete/example/shared/tmp/dragonfly levels=2:2
  keys_zone=dragonfly:100m inactive=30d max_size=1g;
  ...
  server {
    listen      80;
    server_name example.com;
    rewrite     ^   https://example.com$request_uri? permanent;
  }

  server {
    listen 443 ssl default deferred;
    ssl on;
    ssl_certificate /etc/ssl/example.com.crt;
    ssl_certificate_key /etc/ssl/server.key;
    ssl_session_cache shared:SSL:1m;
    server_name example.com *.example.com;
    root /home/pete/example/current/public;
    passenger_enabled on;

    location /media {
      proxy_pass http://10.102.66.4:443;
      proxy_cache dragonfly;
      proxy_cache_valid      200  30d;
      break;
    }
  }
}

DB SERVER NGINX.CONF:

user pete;
...
http {
  sendfile on;
  ...
  keepalive_timeout 65;
  types_hash_max_size 2048;
  ...
  large_client_header_buffers 4 16k;

  server {
    listen 443 ssl;
    ssl_certificate /etc/ssl/example.com.crt;
    ssl_certificate_key /etc/ssl/server.key;

    location / {
      try_files $uri @app;
    }

    location @app {
      proxy_set_header host $Host;
      proxy_redirect off;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      root /home/pete/example/shared/;
    }
  }
}

DRAGONFLY.RB:

 require 'dragonfly'
 app = Dragonfly[:images]
 app.configure_with(:imagemagick)
 app.configure_with(:rails)

 if defined?(ActiveRecord::Base) 
   app.define_macro(ActiveRecord::Base, :image_accessor) 
   app.define_macro(ActiveRecord::Base, :file_accessor) 
 end

我做了什么:

'chown -R pete:pete / home / pete / example / current / public',权限看起来正确。

重启服务器/ nginx / ruby​​ / etc ......

添加'large_client_header_buffers 4 16k;'到nginx.conf

错误/日志:

CHROME CONSOLE:

 Failed to load resource: the server responded with a status of 400 (Bad Request)

NGINX ERROR.LOG(是的......我知道它说'警告')

 2015/06/25 11:49:11 [warn] 25591#0: *345 a client request body is buffered to a temporary file /var/lib/nginx/body/0000000002, client: 173.204.167.103, server: example.com, request: "POST /offices/1-big-o/users/1-peterb HTTP/1.1", host: "test.example.com", referrer: "https://test.example.com/offices/1-big-o/users/1-peterb/edit"

NGINX ACCESS.LOG:

 [25/Jun/2015:11:49:14 -0700] "GET /media/W1siZiIsIjIwMTUvMDYvMjUvMTFfNDlfMTFfNDcxXzhfYml0X21scF9vY19fX2xvY2tlX3R1bWJsZXJfYnlfbmlnaHRzaGFkZTQyNF9kNXppdmpmLmpwZyJdXQ HTTP/1.0" 400 681 "https://test.example.com/offices/1-big-o/users/1-peterb/edit" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.125 Safari/537.36

curl -k https://10.102.66.4:443

 <html>
 <head><title>403 Forbidden</title></head>
 <hr><center>nginx/1.4.6 (Ubuntu)</center>
 </body>
 </html>

更新1:

似乎INSTEAD在DB服务器上保存我的文件,它将它们本地保存到我的应用服务器。文件结构是正确的......只是错误的服务器。

1 个答案:

答案 0 :(得分:0)

由于dragonfly在应用服务器上运行,因此将它们保存在那里。如果您希望反向代理可以访问它们,您可以设置nfs或其他一些文件共享系统。然后文件可以在本地保存,但可以由代理服务器访问。