Configuring an asset host

railsmachine-ops edited this page Nov 30, 2010 · 4 revisions

If you use tools like PageSpeed or YSlow, one common recommendation is "Serve static content from a cookieless domain".

For this page, we'll assume your app is example.com, that you're going to have an asset host assets.example.com, and that you use a multistage environment.

Rails

This is really easy in Rails. Just update your config/environments files:

# in config/environments/staging.rb
# Enable serving of images, stylesheets, and javascripts from an asset server
config.action_controller.asset_host = "http://assets.staging.example.com"

# in config/environments/production.rb
# Enable serving of images, stylesheets, and javascripts from an asset server
config.action_controller.asset_host = "http://assets.example.com"

Just make sure that your views use the the asset helpers, like:

  • stylesheet_link_tag
  • javascript_include_tag
  • image_tag

DNS

You need to setup the following A records using example.com's DNS admin:

  • assets.example.com -> example.com's IP
  • assets.staging.example.com -> staging.example.com's IP address

These probably can be CNAME records too.

Moonshine

It's nearly as simple to setup in moonshine. Your existing config/moonshine/staging.yml might include something like this:

:domain: staging.example.com

We just need to update this section to look like:

:domain: staging.example.com
:domain_alias:
- assets.staging.example.com

Now you can deploy and confirm on staging:

cap staging deploy

And if everything is rad, onwards to production!

cap production deploy

Fortunately, the moonshine side is pretty easy too. We're basically going to update the main virtual host for you app to have alias, ie it will respond to another name. It's configured in