1
votes

I got activestorage to work with my rails 5.2 app on localhost.

class Course < ApplicationRecord
  has_many_attached :files

I'm using direct-upload and storing to local storage. But when I deploy to the staging server I hit two problems:

Upload is broken

Of the three HTTP requests needed to upload a file:

  1. POST /rails/active_storage/direct_uploads HTTP/1.1
  2. PUT /rails/active_storage/disk/eyJfcmFpbHMi...3aff HTTP/1.1
  3. POST /course/1908/file_update HTTP/1.1

the second request never get's a response. The response should be a simple 204 No Content, but instead it runs into a timeout.

the server setup is:

  1. nginx revers proxy on on machine calls
  2. apache on another machine, which runs
  3. passenger

I can see in the logfile that rails writes that the response for the second request is actually quite fast:

Started PUT "/rails/active_storage/disk/eyJfcmFpbHMi...
...
Disk Storage (0.8ms) Uploaded file to key: 82L8qxveeux.. (checksum: ..J1BK==)
Completed 204 No Content in 2ms (ActiveRecord: 0.0ms)

Download is broken

When I attache a file to a course via the rails console, and then try to download the file, I recieve an empty file with the right filename.

Again, the rails logfile seems ok:

Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOn....
...
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)

but somewhere between nginx, apache and passenger the body of the response is lost.

Any ideas what could be at fault here?

1

1 Answers

1
votes

Upload is broken

found no explanation but a solution at Nginx reverse proxy causing 504 Gateway Timeout

replace

proxy_set_header Connection "upgrade";

with

proxy_set_header Connection "";

and the timeout goes away, uploading files works.

Download is broken

I found an unexptected Heder in the HTTP response, containing the path of the uploaded file in local storage:

X-Sendfile: /var/www/.../storage/Rj/9o/Rj9oZL9W1jsHrnS7YJyw5

googling X-Sendfile I found an apache module

https://tn123.org/mod_xsendfile/

that takes this Response header and the send the file as the response body. you can install it on ubuntu / debian with

apt install libapache2-mod-xsendfile

and configure it in apache:

XSendFile On
XSendFilePath /var/www/virthosts....

It seems activestorage uses this by default, but it's not mentioned anywhere in the documentation.