【问题标题】:How do I save Dragonfly Images to a remote server using NGINX reverse proxy?如何使用 NGINX 反向代理将 Dragonfly 图像保存到远程服务器?
【发布时间】:2015-06-25 19:50:49
【问题描述】:

问题:

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

背景:

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

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

应用服务器 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;
    }
  }
}

数据库服务器 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 控制台:

 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:

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

【问题讨论】:

  • proxy_pass 设置为什么?
  • 远程服务器局域网ip

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


【解决方案1】:

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

【讨论】:

  • 没有办法让 nginx 将上游转发到我的远程服务器吗?
  • 是的,这应该是可能的。我以为您表示您希望文件位于数据库服务器上;所以我认为您想直接从数据库服务器为它们提供服务。
  • 对不起。是的,这是我想要服务/存储的所有用户生成的内容。我的所有其他资产都在服务器外预编译并上传到每个应用服务器
猜你喜欢
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
相关资源
最近更新 更多