使用nginx/puma的Rails 5.2.1 ActiveStorage文件下载被截断 [英] Rails 5.2.1 ActiveStorage file downloads with nginx/puma are truncated

查看:364
本文介绍了使用nginx/puma的Rails 5.2.1 ActiveStorage文件下载被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个 ActiveStorage 项目在开发(仅适用于puma)上工作正常,但是在生产中(nginx/puma)上,我对于下载显示为截断文件的大文件有问题 strong>.

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.

例如,上传的文件大小为24.1 MB,则下载的文件大小为5 MB(被截断).

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

我主要上传pdf文件,上传的文件完整(已在服务器上检查)&预览效果很好.

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

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

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

使用rails_blob_path(document.doc, disposition: :inline)

我怀疑puma或nginx中的选项或参数使下载完整文件失败.到目前为止,我在日志中看不到任何错误.

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.

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;
}

设置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

推荐答案

使用proxy_http_version 1.1;

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

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

这篇关于使用nginx/puma的Rails 5.2.1 ActiveStorage文件下载被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆