You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 7 Active Storage搭配Nginx图片加载中断,报net::ERR_HTTP2_PROTOCOL_ERROR 200

Active Storage图片无法完整加载,报错net::ERR_HTTP2_PROTOCOL_ERROR 200(Nginx反向代理Puma场景)

环境信息

  • Ruby版本:3.0.5
  • Rails版本:7.0.5
  • 部署架构:Nginx反向代理Puma(通过Unix套接字通信)

问题现象

通过Nginx反向代理访问应用时,Active Storage托管的图片无法完整加载,浏览器控制台报错net::ERR_HTTP2_PROTOCOL_ERROR 200。直接启动Rails服务器(不经过Nginx)时,图片可正常加载。尝试修改Active Storage路由模式为:rails_storage_proxy后,问题仍未解决。

相关配置

Rails应用启动命令

RAILS_ENV=production /usr/share/rvm/wrappers/ruby-3.0.5/rails server -b /var/www/***/puma.sock

Rails生产环境配置(config/environments/production.rb)

require "active_support/core_ext/integer/time"

Rails.application.configure do

  config.cache_classes = true

  config.eager_load = true


  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true


  config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?


  config.assets.compile = true
  
  config.log_level = :info

  config.log_tags = [ :request_id ]


  config.action_mailer.perform_caching = false

  config.i18n.fallbacks = true

 
  config.active_support.report_deprecations = false

  config.log_formatter = ::Logger::Formatter.new

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false

  config.active_storage.service = :local

  config.active_storage.variant_processor = :mini_magick

  config.action_mailer.perform_deliveries = true

  config.action_mailer.default_url_options = { host: '***.com' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    port: 587,
    domain: '***.com',
    address: 'mail.***.com',
    user_name: ENV['EMAIL_USER'],
    password: ENV['EMAIL_PWD'],
    authentication: 'plain',
    enable_starttls_auto: true
  }


  config.serve_static_assets = false
  config.assets.compress = true

end

Nginx配置

upstream app {
  server unix:///var/www/***/puma.sock fail_timeout=0;
}

server {
    listen 80;
    server_name ***.com www.***.com;
    return 302 https://***.com$request_uri;
}

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;
  
  server_name ***.com www.***.com;
  include snippets/ssl-certs.conf;
  include snippets/ssl-params.conf;

  # Ruby configuration
  root /var/www/***/public;  

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }


  location / {
    proxy_pass http://app;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-NginX-Proxy true;

    proxy_read_timeout 120;
  }

  
  location ~ ^/(500|404|422).html {
    root /var/www/***/public;
  }

  error_page 500 502 503 504 /500.html;
  error_page 404 /404.html;
  error_page 422 /422.html;

  client_max_body_size 4G;
  keepalive_timeout 30;
}

已尝试的解决方案

  • 跳过Nginx直接启动Rails:执行rails s -b 0.0.0.0 -e production,图片加载正常
  • 修改Active Storage路由模式:在Rails配置中添加config.active_storage.resolve_model_to_route = :rails_storage_proxy,问题未得到解决

内容的提问来源于stack exchange,提问作者franchioping

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:39:54