Domain mapping and pull CDN's (8 posts)

  1. Ryan Hellyer
    Posted 4 years ago #

    I have a domain mapped website. It is currently functioning well, but I want to add support for a pull CDN. I'm attempting to use Amazon CloudFront, but am a little stumped on one issue.

    I've set CloudFront to pull from the non domain mapped URLs. However the domain mapping plugin (Donnacha's) is redirecting the upload folder URLs to the domain mapped URL, which means that the pull CDN actually caches a page which redirects to another one. You can see the issue in the following CloudFront URL:


    That file pulls from the following URL:

    Which is domain mapped to this URL:

    I could setup a separate CloudFront deployment for each mapped domain, but that would be a pain to do every time I add a new site.

    Any ideas on what the best way to approach this is? I'm assuming I'm not the first person to stumble across this issue.

  2. Ryan Hellyer
    Posted 4 years ago #


    I came up with dumb solution that would never work.

  3. Ryan Hellyer
    Posted 4 years ago #

    I'm wondering now if I need to find some way to push my files to Amazon S3 first. That way I can pull them from a separate location that can't be interfered with by the domain mapping.

  4. Ryan Hellyer
    Posted 4 years ago #

    I may be at risk of look like an idiot for talking to myself, but in case someone else finds this and is stuck on the same issue ...

    I had a Eureka moment over night. The simplest solution I can think of, is to rewrite the URLs on the pages to point to the blog.dir folder. The CDN would then pull everything from that one folder (perhaps modrewritten to remove the ugly "wp-content/blog.dir/" naming system). That should theoretically solve the problem.

    I also considered shunting stuff to Amazon S3 via S3cmd. But I was concerned about what would happen if Cloudfront cached a file before it found it's way to S3, so haven't bothered implementing that. There would presumably be a gap in time between S3cmd sync'd up with S3 and your live server, and if someone visited a page during that time, then it could cause image load failures.

  5. I'm fairly sure most people are setting up a cloudfront per-site actually :/ Same reason caching gets set up per site. It's one of those things that has to get customized per-site, like we would per separate site.

  6. Ryan Hellyer
    Posted 4 years ago #

    No need to do it per site if I can do it across all of them in a seamless fashion though.

    I'm still having issues trying to do it with the blog.dir folder directly as I suck at rewrites unfortunately (particularly in NGINX).

  7. Ryan Hellyer
    Posted 4 years ago #

    I seem to have it sorted now. I'm sync'ing with Amazon S3 via the S3CMD command line utility. I run a cron job once per minute which does the sync'ing, but I should be able to force it to sync every time a file is uploaded too via an action hook.

    I then point a CloudFront distribution at the S3 bucket, then I point a domain at that CloudFront distrubution.

    I then wrote a custom plugin to rewrite the URLs on the page, to point everything at the new CDN URL's.

    The only thing missing is a redirect from the /files/ URLs to the fancy new URLs. I haven't figured out how to do that yet, but it shouldn't be too difficult. Just a matter of working out how rewrites work in NGINX.

    The final result can be seen at http://ryanhellyer.net/. I'm manually sync'ing up the wp-includes and wp-admin folders now, so there's a couple of smileys and whatnot missing, but it's otherwise fully functional. I also appear to have no browser-caching, presumably due to a failure in how I setup S3 I guess.

  8. Frederick Townes
    Posted 4 years ago #

    For what it's worth, the next release of W3TC supports network-wide policies for CDN. It's worth checking out and has been in the beta for some time. Reach out if you're interested.

Topic Closed

This topic has been closed to new replies.

About this Topic