Performance

Setting Up a CDN for WordPress: What It Does and How to Configure It Properly

Setting Up a CDN for WordPress: What It Does and How to Configure It Properly

If your visitors come from different parts of the world, physical distance matters more than most people realise. A visitor in Sydney loading a site hosted in London waits for data to travel halfway around the planet and back, for every request. Even with a fast server, that distance adds noticeable delay.

A content delivery network, or CDN, solves this by keeping copies of your site’s files on servers around the world. Visitors get files from a nearby location instead of your origin server. The result is faster loading, less strain on your hosting and better resilience during traffic spikes.

CDNs are one of the most effective performance improvements for many WordPress sites, but they need careful configuration. Cache the wrong thing, and you can show one customer’s cart to another, or serve outdated pages for hours. Here’s how to set one up properly.

How a CDN works

A CDN is a network of servers, called edge servers or points of presence, located in data centres around the world. When a visitor requests a file:

  1. The request goes to the nearest edge server.
  2. If the edge server has a cached copy, it serves it immediately.
  3. If not, it fetches the file from your origin server, serves it and stores a copy for the next visitor.

Over time, popular files are served almost entirely from the edge. Your origin server only handles requests the CDN can’t answer.

What a CDN improves

  • Latency. Shorter distances mean faster responses, especially for international visitors.
  • Server load. The CDN handles most static file requests, so your server does less work.
  • Bandwidth costs. Many hosts charge for bandwidth; serving files from the CDN reduces it.
  • Traffic spikes. The CDN absorbs sudden surges, like a viral post or a sale.
  • Security. Many CDNs include DDoS protection and a web application firewall.

What a CDN doesn’t fix: a slow origin server for uncached requests, heavy JavaScript or a bloated database. It speeds up delivery, not the work your server does to build pages.

Two main types of CDN setup

Pull zone CDN with a separate hostname

You create a CDN hostname, such as cdn.example.com, that pulls files from your site. WordPress then rewrites URLs for static assets, like images, CSS and JavaScript, to use the CDN hostname. Your HTML is still served by your origin server.

This setup is straightforward and works with any DNS provider. A plugin or your caching plugin usually handles the URL rewriting.

DNS-level CDN proxy

You point your domain’s DNS to the CDN provider, which then proxies all traffic to your site. Every request passes through the CDN, which caches static files automatically and can also cache HTML pages if configured.

This approach often includes firewall and DDoS protection, and makes full-page edge caching possible. It requires changing your DNS, usually moving DNS management to the provider or using their nameservers.

For most WordPress sites, I prefer the DNS-level approach, because it covers everything, includes security features and doesn’t require URL rewriting.

Step 1: Start with static assets

Begin by caching static files: images, CSS, JavaScript and fonts. These rarely change and are the same for every visitor, so they’re safe to cache aggressively.

Set long cache lifetimes for these files at the CDN and in the browser. WordPress adds version numbers to theme and plugin asset URLs, such as style.css?ver=1.4, so when a file changes, the URL changes and the CDN fetches the new version. Make sure your CDN treats query strings as part of the cache key, or versioned files may not update.

Step 2: Configure HTTPS correctly

With a DNS-level CDN, there are two connections: visitor to CDN and CDN to your origin. Both should be encrypted.

Use the strictest SSL mode your setup supports, where the CDN connects to your origin over HTTPS and verifies the certificate. Some CDNs offer a “flexible” mode where the CDN connects to your origin over plain HTTP. Avoid it. It leaves traffic between the CDN and your server unencrypted and often causes redirect loops in WordPress, because WordPress sees HTTP requests while visitors use HTTPS.

Step 3: Decide whether to cache HTML

Caching HTML pages at the edge gives the biggest speed improvements. Visitors anywhere in the world can get full pages in milliseconds. But it’s also where things go wrong.

When full-page edge caching works well

  • Blogs and content sites where most visitors are anonymous.
  • Business sites with mostly static pages.
  • Sites with traffic spikes that would overwhelm the origin.

What must never be cached at the edge

  • Logged-in users’ pages, including the admin.
  • WooCommerce cart, checkout and account pages.
  • Pages with personalised content, like membership dashboards.
  • Requests with session or cart cookies.
  • Form submissions and API responses with unique data.

A well-configured edge cache bypasses caching when WordPress login cookies or WooCommerce session cookies are present, and never caches the admin, wp-login.php, cart, checkout or account URLs.

Some CDNs offer WordPress-specific products that handle these rules automatically and purge cached pages when content changes. If you configure rules yourself, test them thoroughly.

Step 4: Set up cache purging

When you update a post or change your theme, cached copies need to be cleared, or visitors will see the old version.

  • Use a plugin that integrates with your CDN to purge relevant URLs automatically when content is published or updated.
  • Purge the entire cache after theme changes or major updates.
  • Avoid purging everything constantly, which defeats the purpose of caching.

If changes don’t appear after an update, an unpurged edge cache is the usual suspect.

Step 5: Protect your origin

With a DNS-level CDN, visitors should only reach your site through the CDN. If attackers discover your origin server’s IP address, they can bypass the CDN’s caching and security.

  • Restrict your origin server to accept web traffic only from your CDN’s IP ranges, using firewall rules.
  • Avoid exposing the origin IP through DNS records for subdomains that bypass the CDN, or through email sent directly from the web server.

Step 6: Preserve visitor IP addresses

When traffic passes through a CDN, your server sees the CDN’s IP address instead of the visitor’s. That breaks security plugins, rate limiting, logs and anything that relies on visitor IPs.

CDNs pass the real visitor IP in a header. Configure your web server to trust that header only from the CDN’s IP ranges. On Nginx, this uses the real IP module:

Use your CDN’s actual IP ranges and header name. Never trust the header from arbitrary sources, or attackers can fake their IP addresses.

Step 7: Optimise images at the edge

Many CDNs offer image optimisation: resizing, compression and conversion to WebP or AVIF on the fly. This can save a lot of work on your server and storage. Check how it interacts with your WordPress image setup, so you don’t optimise images twice.

Common CDN mistakes

  • Caching cart or checkout pages, leading to customers seeing each other’s data.
  • Using flexible SSL, causing redirect loops and insecure origin connections.
  • Ignoring query strings in cache keys, so updated assets don’t refresh.
  • Not purging after changes, leaving visitors with stale content.
  • Breaking visitor IPs, so security tools block the CDN itself or can’t identify attackers.
  • Leaving the origin exposed, allowing attackers to bypass the CDN.

Testing your CDN

After setup:

  1. Check response headers to confirm assets are served from the CDN. Most CDNs add a header showing whether a request was a cache hit or miss.
<code>curl -sI https://example.com/wp-content/themes/mytheme/style.cssgrep -i cache</code>
  1. Load pages from different locations using online speed testing tools.
  2. Test logged-in behaviour: log in and confirm you see the admin bar and personalised content.
  3. Test WooCommerce end to end with a cart and checkout in a private window.
  4. Update a post and confirm the change appears for logged-out visitors.

Frequently asked questions

Do I need a CDN if all my visitors are local?

The latency benefit is smaller, but a CDN still reduces server load, absorbs traffic spikes and often adds security. For local businesses with a host nearby, the gains are modest but worthwhile.

Can a CDN break my site?

Misconfigured caching can, especially for dynamic pages. Start with static assets, add HTML caching carefully with proper exclusions and test thoroughly.

Does a CDN help SEO?

Indirectly. Faster pages improve user experience and Core Web Vitals, which support SEO. A CDN won’t help content rank by itself.

Will a CDN make my admin faster?

Usually not much. Admin pages are dynamic and shouldn’t be cached. Admin speed depends on your origin server, database and plugins.

Final thoughts

A CDN brings your site closer to your visitors, reduces strain on your server and adds a layer of protection. Start with static assets, use strict HTTPS, add edge caching for HTML only with careful exclusions, purge caches when content changes and protect your origin. Configured properly, a CDN is one of the best value performance upgrades a WordPress site can get.