Rails Active Storage: Serve your images over HTTPS

Stacie Taylor-Cima
Nov 8, 2018 · 2 min read

Are you building a Rails app? Are you trying to load resources over HTTPS? Are you using AWS S3 storage? Are you getting Mixed Content errors? If you said yes to more than one of those questions, this article is for you.


I recently added an upload image feature to an app where the user clicks an upload button, selects an image from their local storage, then the image is stored in and served from an AWS S3 bucket.

After uploading new photos I was getting the following error telling me that the image was not secure:

Mixed Content: The page at ‘https://staging.myapphere/resources?q%5Bidentifier_or_title_or_representations_text_cont_all%5D=lala&q%5Bassignments_user_id_eq%5D=' was loaded over HTTPS, but requested an insecure image ‘http://staging.myapp/rails/active_storage/blobs/theimageiuploaded.jpg'. This content should also be served over HTTPS.

I needed to figure out how to serve these images via HTTPS!


Naturally, I began searching things like, “serve your S3 images over HTTPS”. After all, that’s exactly what I was trying to do. This lead me on a wild goose chase through AWS’s documentation. Articles with that exact title walked me through several processes where I was editing S3 bucket policies or implementing a process using AWS CloudFront. I still couldn’t figure it out.

That’s when a mentor of mine suggested that S3 was already serving my image with HTTPS by default and that the problem was that the Active Storage URL helper was just not generating the “s” in the URL.


The solution: add the following line to a config/initializers/force_ssl.rb file:

if Rails.application.config.force_ssl  
Rails.application.routes.default_url_options[:protocol] = ‘https’
end
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store