Rails 7 Active Storage in nginx - images stop loading halfway trough with net::ERR_HTTP2_PROTOCOL_ERROR 200

knsnq2tg  于 2023-08-03  发布在  Nginx
关注(0)|答案(1)|浏览(574)

由于某些原因,Active Storage映像未完全加载:Image Not Loading
我正在运行Ruby 3.0.5和Rails 7.0.5
我用这个命令启动我的rails应用程序:

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

字符串
下面是我的Rails环境:

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作为代理,这是我的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 s -b 0.0.0.0 -e production


图像加载正确
再次在nginx中运行,我尝试使用以下命令更改rails主动存储模式:

config.active_storage.resolve_model_to_route = :rails_storage_proxy


但它什么也没做

tjrkku2a

tjrkku2a1#

我看了浏览器中的请求。我收到这些错误:
Firefox中的NS_ERROR_NET_PARTIAL_TRANSFER
net::ERR_HTTP2_PROTOCOL_ERROR 200 chrome
请求的总时间为350ms,这导致我使用这个位置块禁用代理缓冲:

location ~ ^/rails/active_storage/disk {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://myapp;

    proxy_buffering off;

    expires max;
  }

字符串
这样就解决了问题

相关问题