【问题标题】:Rails 5.2.1 ActiveStorage file downloads with nginx/puma are truncated使用 nginx/puma 下载 Rails 5.2.1 ActiveStorage 文件被截断
【发布时间】:2019-04-21 19:48:07
【问题描述】:

我的第一个 ActiveStorage 项目在开发(仅限 puma)上运行良好,但在生产(nginx/puma)上,我在下载显示为 截断文件 的大文件时遇到问题强>.

例如,一个大小为 24.1 MB 的上传文件提供 5 MB(截断)的下载。

我主要上传 pdf 文件,上传的文件是完整的(在服务器上检查)并且预览效果很好。

所有环境都使用config.active_storage.service = :local

config/storage.yml

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

下载地址是使用rails_blob_path(document.doc, disposition: :inline)生成的

我怀疑 puma 或 nginx 中有一个选项或参数导致下载完整文件失败。截至目前,我在日志中看不到任何错误。

/etc/nginx/sites-available/default.conf

upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:/home/deploy/rails/shared/sockets/puma.sock fail_timeout=0;
}

server {
        listen 80;

        root /home/deploy/rails/public;

        try_files $uri/index.html $uri @app;

        # Make site accessible from http://localhost/
        server_name localhost;

        location @app {
            proxy_pass http://app;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
        }

        error_page 500 502 503 504 /500.html;
        keepalive_timeout 10;
        client_max_body_size 4G;
}

Rails 配置/puma.rb

# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
# Change to match your CPU core count
if rails_env == 'production'
  workers 2
else
  workers 1
end

# Min and Max threads per worker
threads 1, 6

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"

if rails_env == 'production'
  # Set up socket location
  bind "unix://#{shared_dir}/sockets/puma.sock"

  # Logging
  stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true

  # Set master PID and state locations
  pidfile "#{shared_dir}/pids/puma.pid"
  state_path "#{shared_dir}/pids/puma.state"
  activate_control_app

  on_worker_boot do
    require "active_record"
    ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
    ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
  end
end

【问题讨论】:

    标签: nginx ruby-on-rails-5 rails-activestorage


    【解决方案1】:

    使用proxy_http_version 1.1;

    默认情况下,nginx 使用 HTTP 1.0 进行代理,不支持分块传输编码。

    http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version

    【讨论】:

    • 感谢@amiuhle,解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多