---
title: "Define Default URL for ActiveStorage to fix Mixed Content Error"
description: "Define default URL for ActiveStorage links and redirects to fix random Mixed Content Error in Ruby on Rails Projects."
url: "https://qameta.com/posts/define-default-url-for-active-storage-to-fix-mixed-content-error/"
---

# Define Default URL for ActiveStorage to fix Mixed Content Error

Using ActiveStorage with external services like AWS S3 for saving and surfing
attachments like images is great. The setup is well explained in [Active Storage
Overview](https://guides.rubyonrails.org/active_storage_overview.html) in [Ruby
on Rails Guides](https://guides.rubyonrails.org).

But if you start using it, you will maybe encounter some problems, which are not
that easy to find or debug, as they mostly appear in production. One of the
problems we encountered was a [Mixed
content](https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content)
related to ActiveStorage. This was not a big problem at first, as you get only a
warning in a browser with default configuration. But after we added an iOS App,
which was using
[WkWebViews](https://developer.apple.com/documentation/webkit/wkwebview), some
random redirects to AWS S3 images stopped working. We couldn't even see
connections for these images to ActiveStorage endpoint of our [Ruby on
Rails](https://rubyonrails.org) Application in logs, which made it harder to
debug.

If you want a deep dive regarding URLs in Ruby on Rails, check this post:
[Default URL Options in Ruby on
Rails](https://qameta.com/posts/default-url-options-in-ruby-on-rails/).

## Find the problem

After some analysis we found some image redirect links to load with "HTTP" and
most with "HTTPS". The next step was to figure out, why it is happening and how
to fix it.

## Understand it

The first simple step for a developer is "ask a search engine" (Google/Bing/....).
But the results were fuzzy and most of them were covering unrelated topics or
were just "it works now", which I consider being a problem for most developers
just trying the fix without understanding the issue, finding the right solution
and maybe write a test for it.

### Documentation

I've checked the documentation for
[ActiveStorage](https://edgeapi.rubyonrails.org/classes/ActiveStorage.html) and
the related page in [Ruby on Rails
Guides](https://edgeguides.rubyonrails.org/active_storage_overview.html), but
couldn't find anything useful.

### Project Issues

For me, it looked like an issue, that someone else might have had before. Let's
check [GitHub issues for Ruby on Rails](https://github.com/rails/rails/issues)
first. There I found this issue, which told me, that someone else had a similar
problem and he tried to fix it by a pull request: [Ruby on Rails Issue
42947](https://github.com/rails/rails/pull/42847). This pull request was closed.
But I got a slight idea of where to look for the implementation, to figure out,
how to fix it in our project.

### Check related Source Code

The next step was to check the source code, which might tell me, where I have to
set the default URL options for the project. It was in the class [DiskService on
Line
168](https://github.com/ceritium/rails/blob/68a695212e3504dd82ceb1ab60930b41c5b508ec/activestorage/lib/active_storage/service/disk_service.rb#L168):

```ruby
def url_helpers
  @url_helpers ||= Rails.application.routes.url_helpers
end
```

## Solution

As the project was more of a monolith, for me it was enough to set the default
URL options for routes and therefore define it in for ActiveStorage too. As it was
only relevant for production, I've added this on the top of
*config/environments/production.rb*:

```ruby

Rails.application.routes.default_url_options = { host: "www.my.project",
                                                 protocol: "https" }
```

After deploying the application to production the problem was gone and
ActiveStorage used always the right URL *https://www.my.project/*.

## Summary

Sometimes it's not that easy to find a problem, which seems to be random and
maybe not a problem on all devices, operating systems or browsers. You need a
deep understanding what might be the reason for the problem and where look for
the reason.

As most problems were encountered by other developers, you can try to search for
it on web, [stackoverflow](https://stackoverflow.com), check the documentation,
check project issues, or to be sure check the source code itself, which is the
most secure documentation you can have.

If you want a deep dive regarding URLs in Ruby on Rails, check this post:
[Default URL Options in Ruby on
Rails](https://qameta.com/posts/default-url-options-in-ruby-on-rails/).
