Skip to content

CDN (content delivery network)

A CDN is a network of servers in many places that keeps copies of a site's files and answers each visitor from a server near them. On a WordPress site it mostly serves images, stylesheets and scripts. It does not make a slow server build pages any faster.

By
WP Ministry
Published

In short

  • A CDN shortens the distance between your files and your visitors, and takes requests off your hosting server.
  • It sits in front of the site or beside it, so a fault at the CDN can look like a fault on the site.
  • It caches files, and sometimes whole pages. A page built for one person must be kept out of it.

MDN describes a CDN as "a group of servers spread out over many locations" that store duplicate copies of data, so that a request is answered by a server close to the person asking. The hosting server it copies from is called the origin.

On a WordPress site the copies are mostly static files. WordPress's own guide names images, JavaScript, CSS and theme files, and says that handing their delivery to a CDN is faster and takes load off the server. Some CDNs, it adds, can cache a page's whole HTML.

There are two ways to connect one. For the whole site, the domain's DNS points at the CDN and every request passes through it. For static files only, the files are given addresses on a second hostname, such as static.example.com, that points at the CDN.

A CDN is the only one of a site's caches that lives outside your server. A page cache stores finished pages, usually on the server itself, though a CDN can hold one too. The object cache, transients and OPcache all work inside the server while WordPress builds a page, and a CDN cannot stand in for any of them.

Where you meet it

  • Your domain's DNS. A record, such as a CNAME, that points at the CDN and not at your host.
  • The addresses of your files. Images and stylesheets that load from a different hostname than the page.
  • Response headers. The HTTP standard says the presence of an Age header means a response came from a cache and was not produced by the origin for this request. A CDN's documentation names any header of its own.
  • Error pages. With a CDN in front of the site, an error page can carry the CDN's name and not your host's.

What goes wrong

  • An old file after a change. The CDN goes on serving its copy until the copy expires or is purged. WordPress's hosting handbook warns that unless every location is purged, "people in certain areas may get old results". How to set up a CDN for WordPress covers purging.
  • A 502 error. The CDN asked your server for a page and got no usable answer. How to fix the 502 Bad Gateway error shows how to tell which side failed.
  • Missing styles or a redirect loop after adding one. The CDN takes the visitor's https connection and asks your server over plain http, so WordPress writes http addresses, or the site redirects in a loop. See how to fix mixed content warnings and how to fix ERR_TOO_MANY_REDIRECTS.
  • A personal page is cached. A CDN is a shared cache: it stores one response and reuses it for many visitors. MDN warns that personal content stored this way can reach other users. Carts, checkouts and account pages must stay out. See how to fix a WooCommerce checkout that is not working.
  • It is expected to fix a slow server. Pages the CDN does not cache are still built by WordPress on the origin. How to speed up a slow WordPress site puts a CDN after the hosting, the page cache and the images.

Common questions

Does a CDN replace a page cache?

Only if it is set up to store whole pages, which WordPress's guide says some CDNs can do. The same guide says a CDN is most effective when used with a caching plugin. The plugin spares the server the work of building pages, and the CDN delivers the files.

Can a CDN make a site slower?

It can. MDN notes that loading a few files from a third-party CDN hostname costs the browser an extra lookup and connection. WordPress's hosting handbook adds that when pages are not cached at the CDN, the wait for the first byte can grow, because the request still has to go on to the origin.

Not sure what is wrong?

Tell us what you see. We reply with the cause and a fixed quote, and the diagnosis is free.