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:
- You point your domain's DNS to the CDN instead of directly to your server.
- A visitor's request goes to the nearest edge server, usually selected through DNS or anycast routing.
- If the edge has a valid cached copy (a hit), it responds immediately.
- 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
| Content | Cache at the CDN? | Notes |
|---|---|---|
| Images, CSS, JS, fonts | Yes, for long periods | Use versioned file names |
| Video and downloads | Yes | Large savings in bandwidth |
| Public HTML pages | Optional | Short TTL plus purge on change |
| API responses | Sometimes | Only public, non-personal data |
| Logged-in pages, carts, checkout | No | Personal 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:
- Prepare the origin: make sure your site works over HTTPS on the VPS and sends correct
Cache-Controlheaders for static files. - Add the site to the CDN and either change your nameservers or create the CNAME record the provider gives you.
- 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.
- Configure cache rules: cache static assets, bypass the cache for admin, login, cart and checkout paths, and for requests with session cookies.
- Restore real visitor IPs: your server will now see CDN addresses. In Nginx, use the
real_ipmodule withset_real_ip_fromfor the CDN's published ranges andreal_ip_headerset to the header the provider documents. This keeps logs, rate limits and tools like fail2ban accurate. - 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. - 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.