Rails 5.2.1 ActiveStorage file downloads with nginx/puma are truncated

China☆狼群 提交于 2019-12-22 10:57:19

问题


My first ActiveStorage project was working fine on development (puma only) but on production (nginx/puma), i have an issue for downloading big files that appear as truncated files.

For instance, an uploaded file sized 24.1 MB gives a 5 MB (truncated) download.

I mostly upload pdf files, uploaded files are complete (checked on server) & the preview works fine.

All environments use the config.active_storage.service = :local

config/storage.yml

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

Download url is generated using rails_blob_path(document.doc, disposition: :inline)

I suspect an option or parameter in puma or nginx that makes downloading the full files fail. As of now I cannot see any error in logs.

/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 config/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

回答1:


Use proxy_http_version 1.1;

By default, nginx uses HTTP 1.0 for proxying, which does not support chunked transfer encoding.

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



来源:https://stackoverflow.com/questions/53374839/rails-5-2-1-activestorage-file-downloads-with-nginx-puma-are-truncated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!