Optimal Static Content Subdomain Configuration

Using subdomains for serving static content on your store will improve both performance and cacheability of static assets. Making this change is very straightforward and can be completed in a few minutes.

In this example, we'll assume the domain is www.example.com.

Configure DNS Records

Create a single DNS record,

Record Type Source Destination
CNAME cdn.example.com www.example.com

! Be aware that DNS records can take up to 72 hours to propagate. You should wait at least 3 days before proceeding with the next steps.

Create Subdomains/Vhosts

You can use either a subdomain or vhost (read more about domain groups, vhosts and subdomains) to create the links to the static content. There are merits to each (read the previously linked article to find out), so pick the most appropriate one to suit your needs, for most, subdomains are sufficient.

Subdomain Method

This is the simplest and recommended method of deployment.

cd /microcloud/domains/example/domains/example.com/
ln -s http cdn

Vhost Method

You should have a single vhost for cdn- within each vhost, the http directory should be removed and replaced with a symlink to the respective directory.

cd /microcloud/domains/example/domains/cdn.example.com/
rm http
ln -s ../example.com/http http

Change Magento URLs

Log into your Magento admin and go to System > Configuration > General > Web

Configure the URLs for both unsecure and secure URLs respectively,

Setting Value
Unsecure Base Skin URL https://cdn.example.com/skin/
Unsecure Base Media URL https://cdn.example.com/media/
Unsecure Base JavaScript URL https://cdn.example.com/js/
Secure Base Skin URL https://cdn.example.com/skin/
Secure Base Media URL https://cdn.example.com/media/
Secure Base JavaScript URL https://www.example.com/js/

It is not recommended to change the Secure (HTTPS) URL for JavaScript as this can cause problems within the Magento admin area itself (if your admin URL matches that of the store view you are changing).

Secure the vhost

If using the vhost method, it is recommended to enable padmin/downloader/rss protection](https://www.sonassi.com/help/security/protecting-magento-admin-downloader) (if not already enabled globally).

Strip Cookies From Files

Finally, it is recommended to configure a setting on MageStack to strip cookies from the static and semi-static content (as per the optimal Varnish/PageSpeed guide).

In example.com.location.static.conf

if (-f $request_filename) {
  set $magestack_clear_cookies true;
}

In example.com.location.semi-static.conf

if (-f $request_filename) {
  set $magestack_clear_cookies true;
}

If using the vhost method, these settings will need to be configured on each vhost as appropriate.