使用Amazon S3和Rails Active Storage时丢失附件

时间:2018-03-12 10:14:03

标签: ruby-on-rails nginx amazon-s3 rails-activestorage

我在开发(磁盘存储)时成功使用Active Storage,但是在部署应用程序(Amazon S3存储)时,找不到所有附件。

上传工作没有任何问题 - 文件出现在S3存储桶中,并且创建了活动存储数据库记录。但是,只要我使用.variant()url_for(),就会丢失所有这些文件。

Rails日志并没有告诉我任何事情,好像请求根本没有发生。这让我觉得我的Web服务器配置是错误的。

这是我目前的nginx配置:

upstream my_app {
  server unix:/srv/example.com/current/tmp/sockets/unicorn.sock fail_timeout=0;
}

server {
  listen 80;
  server_name example.com;
  return 301 https://example.com$request_uri;
}

server {
  listen 443 ssl;
  server_name example.com;
  root /srv/example.com/current/public;

  include h5bp/directive-only/ssl.conf;
  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

  include h5bp/basic.conf;
  include h5bp/auth.conf;

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://my_app;
  }

  access_log /var/log/nginx/example.com/access.log;
  error_log /var/log/nginx/example.com/error.log;

  charset utf-8;
  error_page 404 /404.html;
  error_page 502 503 @maintenance;

  if (-f /srv/example.com/shared/maintenance.txt) {
    return 503;
  }

  location @maintenance {
    root /srv/maintenance;
    rewrite ^(.*)$ /index.html break;
  }
}

basic.conf来自此处:https://github.com/h5bp/server-configs-nginx/tree/master/h5bp auth.conf只是HTTP简单身份验证。

Nginx日志显示如下行:

2018/03/12 11:48:32 [error] 9402#9402: *16285 open() "/srv/example.com/current/public/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fe2fe760c83020f37a9fe8f78bcb4fc958744008/test-powerpoint-document.pptx" failed (2: No such file or directory), client: xxx.xx.xxx.xx, server: example.com, request: "GET /rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fe2fe760c83020f37a9fe8f78bcb4fc958744008/test-powerpoint-document.pptx?disposition=upload HTTP/1.1", host: "example.com"

我错过了什么?

1 个答案:

答案 0 :(得分:1)

如果有人遇到同样的问题,我通过评论以下内容来修复我的案例:

# h5bp/location/expires.conf
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|mp4|ogg|ogv|webm|htc)$ {
  access_log off;
  add_header Cache-Control "max-age=2592000";
}

现在所有图片显示和附件下载都没有任何问题。