From this case study you will learn
- which Magento directories move to the CDN and which stay on the server
- how to set up a private S3 bucket with CloudFront and Origin Access Control
- in what order to switch Magento so new files stop landing on the disk
- what you pay for in such a rollout and what is free
The shop ran on Magento 2 and served several storefronts: B2C, B2B and country stores on separate domains. Product images, manuals and content editor files lived on the application disk, next to the PHP code and the database.
The starting point
The media directory grew with every import and took up hundreds of gigabytes. The application disk became a bottleneck: every image left the same server as PHP, and public files were mixed with private ones. The client planned to run several application instances, and a shared local media disk made that impossible.
The goal was not to move the whole shop to the cloud. It was to stop media from loading the application server.
What moved and what stayed
Phase one covered pub/media only. A single media domain, for example assets.sklep.pl, serves every storefront. Images in the HTML use an absolute CDN URL, while the shop keeps running on its own domains.
Moved to the CDN:
media/catalog/product, product images and thumbnails,media/wysiwyg, content editor files,- manuals and other public assets from the media directory.
Stayed on the server:
- HTML, because it depends on the domain and store view,
pub/static, because CSS and JS rely on versioning and nginx rules, a separate project,- the admin panel,
- feeds, customer files and digital products, which must not be public.
Architecture
HTML still comes from the shop server. Media goes through CloudFront from a private S3 bucket. The local disk is now used only for migration and a possible rollback.
Key decisions:
- Private S3 bucket. Public access is blocked.
- CloudFront with Origin Access Control. The bucket serves files to this one distribution only.
- ACM certificate in us-east-1. CloudFront requires a certificate from that region.
- DNS record without a proxy. A proxy in front of CloudFront would double the transfer and lengthen the path.
- Private prefixes return 403. Customer files, downloadable products and imports are not reachable by a known URL.
- Empty prefix in Remote Storage. Magento adds
media/to the object key itself. Amediaprefix would producemedia/media/paths.
If Cloudflare was already caching media, the speed gain is moderate. The origin of the file still changes: it is no longer the application disk.
Data migration
We copied files from production to S3 with sync without --delete, so nothing disappeared locally. The script skipped private directories the CDN would not serve anyway. After the first full copy, just before switching Magento, we ran a delta sync.
Three things need attention during migration:
- Non-UTF-8 file names. AWS CLI rejects keys with invalid bytes, so we found and fixed such files before the copy.
- Millions of small files. We tuned parallel requests so the copy would not slow down the live shop.
- Thumbnails in catalog/product/cache. CloudFront will not generate a thumbnail the way local
get.phpdoes, so they must be copied or regenerated.
Cutover
Changing base_media_url alone is not enough. The shop would show old files from S3 while new uploads and imports kept landing on the disk. That is why the cutover followed a fixed order:
- Media copy complete and verified under the CDN URL.
- Magento writes media to S3:
remote_storage = aws-s3with an empty prefix. base_media_urlpoints to the CDN, one address for every store view.- Configuration cache and full page cache flushed.
- Imports that wrote files by absolute path or through
DirectoryList::PUBfixed. They moved toDirectoryList::MEDIA, otherwise crons and external imports would bypass S3.
The rollback plan was simple: clear base_media_url and return to the local driver, after first downloading from S3 the files created after the cutover.
What stays outside the CDN
Some assets were deliberately left out of phase one:
- Product feeds are generated locally and exposed to partners at known URLs. A later step can publish them to S3.
- Customer files and downloadable products can live in the same bucket, with no CloudFront access. Magento serves them through PHP.
- Sessions, cache and queues do not belong on the application disk. They belong in Redis, Varnish and RabbitMQ.
- Custom files in var/ are not covered by Remote Storage, which handles only media and
import_export.
Costs
You pay for PUT requests during migration and ongoing uploads, S3 storage per GB per month, and transfer from CloudFront to users. Transfer from S3 to CloudFront and the ACM certificate itself are free. The largest one-off cost is usually the first migration: hundreds of gigabytes and millions of objects.
Result
Images no longer leave the same server as PHP and the database. CloudFront keeps files close to users, and with a cache of around 30 days repeat visits do not go back to the origin. B2C, B2B and country storefronts share one media host. Admin uploads, thumbnails and imports land in one place, which is the prerequisite for running several stateless application instances.
The bucket stays private. Only the paths CloudFront deliberately exposes are public.
Summary
The shop stayed where it was. Media is served from private S3 through CloudFront. What makes it work is switching the media URL and S3 writes at the same time, and fixing imports that bypass Magento's standard file API. DNS and CloudFront alone are only half the job.
Write to us