00:00

Reverse Proxy vs CDN: Key Differences

Last updated:

A reverse proxy and a content delivery network (CDN) both stand in front of your servers and both can cache responses. The difference is where they run. A reverse proxy usually sits in your own data centre or cloud region. A CDN runs hundreds of servers around the world, close to your users.

This tutorial explains how they relate, how caching flows from the edge to the origin, and how to control it with headers. For background, see Reverse Proxy and What Is a CDN.

1. What Are a Reverse Proxy and a CDN?

A reverse proxy is a server in front of your application servers that receives client requests, forwards them, and can terminate TLS, cache, compress and route traffic. There is typically one logical proxy tier per region.

A CDN is a globally distributed network of edge servers, called points of presence (PoPs), operated by a provider. Users are routed to a nearby PoP, which serves cached content or fetches it from your origin. In practice, a CDN is a planet-wide fleet of caching reverse proxies.

2. Edge and Origin Architecture

Reverse proxy compared with CDN A user in Mumbai requests a product image from the nearest CDN edge server, which has it cached and answers immediately. A user in Berlin reaches the Berlin edge server, which does not have it cached yet, so the edge fetches it from the origin reverse proxy, which serves it from the application servers. The edge then caches it for the next Berlin user. Mumbai user Berlin user CDN EdgeMumbai · cache HIT CDN EdgeBerlin · cache MISS ORIGIN DATA CENTRE Reverse ProxyTLS · routing · cache App servers on miss
A CDN is a worldwide layer of caching reverse proxies. Your own reverse proxy sits at the origin behind it.

The CDN absorbs most traffic at the edge. Only cache misses and dynamic requests travel to the origin, where your reverse proxy handles them.

3. How a Request Flows

  1. A user in Berlin requests https://cdn.shop.example/img/mouse.jpg. DNS or anycast routing sends them to the Berlin PoP.
  2. The edge checks its cache. On a hit it responds within a few milliseconds and the origin is never contacted.
  3. On a miss it requests the file from the origin, which is your reverse proxy.
  4. The reverse proxy serves it from its own cache or from an application server, with a Cache-Control header such as public, s-maxage=600.
  5. The edge stores the response for 600 seconds and returns it. The next Berlin user gets a cache hit.

Origin shield

With many PoPs, a popular file that expires can cause a miss at every edge at the same moment, and all of those requests hit your origin. Many CDNs offer an origin shield: one designated mid-tier cache that the edges ask first, so the origin receives one request instead of hundreds. Your own reverse proxy cache plays a similar role inside the data centre by protecting the application servers from repeated misses.

4. Key Concepts

Origin vs edge

The origin is the source of truth: your reverse proxy and app servers. The edge is the CDN copy near users.

Cache-Control directives

Freshness comes from the response's Cache-Control header. Every cache, the browser included, honours max-age; a CDN or proxy gives s-maxage precedence when it is present; private keeps a response out of CDNs and proxies; and no-store prevents storing it anywhere.

Invalidation and versioning

Purge a URL through the CDN API, or better, put a version or hash in file names (app.3f9a2c.js) so new releases use new URLs.

Dynamic acceleration

Even uncacheable API calls benefit from a CDN: TLS is terminated near the user, and the CDN keeps warm connections to the origin.

Cache key

The cache key decides which requests share a cached copy, usually the host, path and query string. Including unnecessary parts, such as tracking parameters or every cookie, splits the cache into many copies and lowers the hit ratio.

5. Practical Implementation

Origin reverse proxy (Nginx) with a cache

nginx.conf (origin)
http {
    proxy_cache_path /var/cache/nginx keys_zone=origin_cache:20m max_size=5g inactive=1h;

    server {
        listen 443 ssl;
        server_name origin.shop.example;
        ssl_certificate     /etc/nginx/certs/origin.crt;
        ssl_certificate_key /etc/nginx/certs/origin.key;

        location /img/ {
            root /var/www;                       # static files served directly
            add_header Cache-Control "public, max-age=86400, immutable";
        }

        location /api/ {
            proxy_pass http://10.0.1.11:8080;
            proxy_cache origin_cache;            # respects Cache-Control from the app
            proxy_cache_use_stale error timeout updating;
        }
    }
}

Spring Boot: setting cache headers for the CDN

The application decides what may be cached. In this example the browser may reuse the product for 60 seconds, and the CDN may reuse it for 600.

ProductController.java
package com.example.catalog;

import java.time.Duration;

import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    public record Product(long id, String name, long priceInCents) {
    }

    @GetMapping("/api/products/{id}")
    public ResponseEntity<Product> product(@PathVariable long id) {
        Product product = new Product(id, "Wireless Mouse", 2499);
        return ResponseEntity.ok()
                .cacheControl(CacheControl.maxAge(Duration.ofMinutes(1))   // browsers keep it 1 minute
                        .sMaxAge(Duration.ofMinutes(10))                   // CDN and shared caches keep it 10 minutes
                        .cachePublic())
                .body(product);
    }
}

Output:

Cache-Control: max-age=60, public, s-maxage=600

That header value was generated by running Spring's CacheControl builder (Spring Framework 7) with the settings above. The controller compiles against Spring Web 7 on Java 17.

6. Advantages and Disadvantages

Reverse proxyCDN
Main benefitFull control over routing, security and cachingLow latency worldwide and huge capacity
Traffic protectionLimited by your own bandwidthAbsorbs large traffic spikes and many DDoS attacks
Dynamic contentHandles all application logic routingBest for cacheable content; dynamic calls pass through
DrawbacksUsers far away still see high latencyCost, vendor dependency, cache invalidation complexity

7. Reverse Proxy vs CDN Comparison

AspectReverse proxyCDN
LocationYour data centre or cloud regionHundreds of edge PoPs worldwide
Operated byYour teamA CDN provider
Main purposeFront, route and protect your serversServe content close to users
CachingOptional, one locationCore feature, distributed
Request routingPaths and hosts to backendsUsers to the nearest PoP (DNS or anycast)
Typical examplesNginx, HAProxy, EnvoyCloudflare, Akamai, Amazon CloudFront, Fastly

8. Real-World Use Cases

  • E-commerce: images, scripts and product listings come from the CDN; checkout APIs pass through to the origin reverse proxy.
  • Video streaming: video segments are cached at the edge, keeping origin bandwidth low.
  • Software downloads: large installers are distributed from PoPs near users.
  • Internal apps: an intranet tool used in one office needs only a reverse proxy, not a CDN.

Deciding what to cache

  • Static assets with hashed names: cache for a year at both layers with immutable.
  • Public product and article pages: cache at the CDN for minutes and revalidate with ETag or Last-Modified.
  • Personalised pages and account APIs: mark them private or no-store.
  • Search and listing APIs: short shared caching, such as 30 to 60 seconds, often removes most origin load during traffic spikes.

Review these rules whenever you add an endpoint. One missing private directive can expose one user's data to another.

9. Best Practices

  • Set explicit cache headers: use s-maxage for the CDN and shorter max-age for browsers.
  • Never cache personal data publicly: mark user-specific responses private or no-store.
  • Version static assets so you rarely need purges.
  • Lock down the origin: accept traffic only from the CDN, for example with provider IP lists or a secret header, so attackers cannot bypass it.
  • Serve stale on error: allow stale content when the origin is down to stay available.
  • Monitor the cache hit ratio at both the edge and the origin.

Also plan for failure of the CDN itself. Keep DNS time-to-live values moderate and document how to send traffic straight to the origin, with the reverse proxy sized to absorb the extra load, if the CDN provider has an outage.

10. Summary

  • A CDN is a global network of caching reverse proxies operated by a provider.
  • Your reverse proxy sits at the origin and handles cache misses and dynamic requests.
  • Cache-Control headers from the origin decide what the CDN may cache and for how long.
  • CDNs reduce latency and absorb traffic spikes; reverse proxies give routing and control.
  • Most public websites use both layers together.

Next, compare the reverse proxy with an API-focused entry point in API Gateway vs Reverse Proxy.