Website Performance

What Is a CDN and How Does It Speed Up Your Website?

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Your VPS lives in one data center. Your visitors do not. Someone in the same region gets a quick answer, while someone on another continent waits for every request to cross thousands of kilometres of cable and back. A content delivery network, or CDN, shortens that trip by keeping copies of your content on servers spread around the world, so most requests are answered nearby. This article explains how a CDN works, what it can and cannot do, and how to put one in front of a VPS safely.

How a CDN works

A CDN is a large network of caching servers, often called edge servers or points of presence (PoPs), located in many cities. When you put your site behind a CDN, the process looks like this:

  1. You point your domain's DNS to the CDN instead of directly to your server.
  2. A visitor's request goes to the nearest edge server, usually selected through DNS or anycast routing.
  3. If the edge has a valid cached copy (a hit), it responds immediately.
  4. If not (a miss), the edge fetches the file from your server, called the origin, returns it to the visitor and stores it for the next request.

Your VPS remains the single source of truth. The CDN is a distributed cache in front of it.

Why a CDN makes sites faster

Shorter distance

Light in fibre covers roughly 200 km per millisecond, and real routes are longer than straight lines. Each round trip to a distant server adds noticeable delay, and loading a page takes many round trips: DNS, the TCP connection, the TLS handshake, then the requests themselves. Serving from a nearby edge cuts each of those trips.

Faster connection setup

Visitors complete their TCP and TLS handshakes with the nearby edge rather than your distant origin. The CDN usually keeps warm, reused connections to your origin, so even uncached requests can benefit.

Less load on your server

Every hit at the edge is a request your VPS never sees. Images, CSS, JavaScript, fonts and downloads often make up most requests and most bytes, so offloading them frees CPU, RAM and bandwidth for the dynamic work only your server can do.

Extra protection

Many CDNs absorb traffic floods at the edge, offer web application firewall rules and hide your origin's IP address. That protection only works if you also restrict your server to accept web traffic from the CDN, as described below.

What a CDN caches, and what it should not

ContentCache at the CDN?Notes
Images, CSS, JS, fontsYes, for long periodsUse versioned file names
Video and downloadsYesLarge savings in bandwidth
Public HTML pagesOptionalShort TTL plus purge on change
API responsesSometimesOnly public, non-personal data
Logged-in pages, carts, checkoutNoPersonal data must never be shared

By default, most CDNs cache static file types and pass HTML through to your origin. They also respect your Cache-Control headers, which makes your server configuration the main control panel. For background on headers and TTLs, see our website caching guide.

Setting up a CDN in front of a VPS

The exact screens differ by provider, but the steps are similar:

  1. Prepare the origin: make sure your site works over HTTPS on the VPS and sends correct Cache-Control headers for static files.
  2. Add the site to the CDN and either change your nameservers or create the CNAME record the provider gives you.
  3. Set TLS to full validation between the CDN and your origin, so traffic is encrypted on both legs. A free certificate from Let's Encrypt on the VPS is enough.
  4. Configure cache rules: cache static assets, bypass the cache for admin, login, cart and checkout paths, and for requests with session cookies.
  5. Restore real visitor IPs: your server will now see CDN addresses. In Nginx, use the real_ip module with set_real_ip_from for the CDN's published ranges and real_ip_header set to the header the provider documents. This keeps logs, rate limits and tools like fail2ban accurate.
  6. Lock down the origin: allow ports 80 and 443 only from the CDN's IP ranges, using ufw, nftables or your cloud firewall. Otherwise attackers can bypass the CDN by hitting your IP directly.
  7. Verify: check response headers with curl -I https://yoursite.com/style.css. Most CDNs add a header showing whether the response was a hit or a miss.

Limits worth knowing

  • A CDN does not make slow dynamic pages faster if they cannot be cached. Fix server response time first.
  • It adds another cache layer to purge when content changes, which is why a clear cache invalidation plan matters.
  • If your visitors are all close to your data center, the speed gain is smaller, though offloading and protection still help.

A CDN works best alongside a well-placed origin. Choosing a server location near most of your audience, from our VPS plans, and adding a CDN for everyone else, gives a good balance.

FAQ

Do I need a CDN if my visitors are in one country?

Not always. If your server is close to your visitors, latency is already low. A CDN can still reduce load on the server and add protection, but the speed improvement will be modest compared with a global audience.

Will a CDN cache my dynamic pages automatically?

Usually not. Most CDNs cache static files by default and pass HTML to your origin unless you add rules. Cache dynamic HTML only for anonymous visitors, with short lifetimes, and always exclude carts, checkout and logged-in areas.

Does using a CDN hide my server's IP address?

Only partly. Web traffic goes through the CDN, but your IP can leak through old DNS records, email headers or subdomains that point directly to the server. Restrict ports 80 and 443 to the CDN's ranges so a leaked IP cannot be used to bypass it.

Related guides