Magento Tutorials

How to Configure a Content Delivery Network (CDN) in Magento

Storing media files in a CDN is another alternative supported in Magento. There is no full integration with a specific CDN, and you are responsible for choosing and configuring a CDN on your own. After configuring the CDN, you must complete the configuration from the Admin. The changes can be made at either the global or website level.

When a CDN is selected as media storage, all paths to media on the web store pages are changed to paths to the CDN. The process of retrieving media files stored in a CDN works as follows:

  • When a customer opens a page in the store, the browser addresses the CDN with the request to acquire some media files, such as images
  • If the CDN does not yet have these images in its storage, it requests them from the server, and provides images to the customer’s browser
  • If the CDN already has the required images in storage, it simply serves the requested images to the customer’s browser

Please note that when a CDN is used as media storage, JavaScript may not function properly if the CDN is not in your subdomain. This is a browser security feature.

Setting up a Content Delivery Network for Magento 2

Step 1: On the Admin menu, select Stores > Settings > Configuration

Stores configuration

Step 2: In the panel on the left, under General, select Web

Web Configuration magento 2

Step 3: Click to expand the Base URLs section and:

  • Base URL for Static View Files to your CDN URL for static view files (starting with http://)
  • Base URL for User Media Files to your CDN URL for media files (starting with http://)
Setting up unsecure CDN URLs Magento 2

Step 4: Click to expand the Secure section.

In this section you’ll want to include CDN URLs with an HTTPS origin. 

  • Change Secure Base URL for Static View Files to your CDN URL for static view files (URLs must start with https://)
  • Change Secure Base URL for User Media Files to your CDN URL for media files (URLs must start with https://)
Setting up secure CDN URLs Magento 2

Step 6: When complete, click the Save Config button.