Build a Mini CDN with Nginx in 3 Simple Steps
This guide walks you through creating a lightweight CDN using Nginx on Ubuntu, covering installation, virtual host configuration, cache settings, and testing, while explaining core concepts like origin pull, proxy caching, and header management for educational purposes.
Introduction
Although the term "intelligent cloud distribution" sounds sophisticated, its basic principle is the same as the earliest CDN concepts, essentially an Origin Pull or Push CDN content caching platform.
Early CDN platforms relied on limited static options and could not adapt to failures or dynamic network conditions, making them obsolete today.
You can still build a small CDN yourself to understand its operation, but only in a lab or test environment, not production.
1. Install Nginx Web Server
sudo apt-get install nginx2. Configure Virtual Host
After installing the web server, edit a virtual host file in /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/ to enable quick testing. Remember to disable the firewall so the test server is reachable.
For a full configuration example, see the linked file and place it in the Nginx directory.
Create a cache directory /tmp/cache, grant write permission to the Nginx user, and reload the service.
3. Set Configuration File
3.1 Cache Directory Settings
proxy_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:100m max_size=1g inactive=60m;This command defines the cache directory, sub‑directory levels, shared memory zone ( keys_zone) of 100 MB, and a disk cache size of 1 GB. inactive removes objects not accessed for 60 minutes, freeing space for new cache entries.
3.2 Hostname Settings
listen [::]:80;
listen:80;
#edit your actual site name
server_name www.example.com;Replace www.example.com with any domain or local host you wish to test.
3.3 Origin Configuration
location / {
proxy_pass http://origin.example.com;
proxy_set_header Host $host;
proxy_set_header True-Client-IP $remote_addr;
}The proxy_pass directive forwards all requests for / to the origin server. proxy_set_header sends the correct Host header and the client IP to the origin.
The origin server remains hidden behind the CDN, so adding the client‑IP header is useful for testing.
3.4 Static Object List
location ~* .(jpg|png|gif|jpeg|webp|css|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx) {
...
}This rule matches common static file extensions and applies caching directives such as: proxy_cache_valid 200 301 7d; Responses with status 200 or 301 are cached for 7 days, provided they are accessed at least once every 60 minutes. proxy_cache_min_uses 2; An object must be requested at least twice before it is cached. proxy_cache my_cache; This selects the cache zone defined earlier.
4. Run and Test
Reload Nginx, edit your hosts file to point www.example.com to the CDN IP, and browse the site. Check /tmp/cache for generated cache files.
This method is a practical way to learn caching and CDN fundamentals.
5. Limitations
The simple CDN lacks features such as cookie handling and advanced cache control. The provided configuration has not been run in a real system; you can test it with nginx‑test or submit a pull request on GitHub if you find issues.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
