SoFunction
Updated on 2025-04-10

How to build your own CDN server in Nginx

nginx installation

sudo apt update
sudo apt install nginx

nginx configuration file

/etc/nginx/or /etc/nginx/sites-available/default

Set up cache

Enable caching in Nginx configuration, which allows static content on the CDN server to improve performance and speed.

http {
    # Set cache path under http section    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
                
    # Set the cache expiration time    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;

    # Set the key and cache size used by proxy cache    proxy_cache_key "$scheme$request_method$host$request_uri";

    server {
        #Other server settings        location / {
            # Use proxy cache            proxy_cache my_cache;//Enable cache            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            proxy_cache_bypass $http_cache_control;//http header            proxy_ignore_headers Cache-Control;
            proxy_pass http://backend_server;//backend data source server        }
    }
}

Forward Proxy and Reverse Proxy are two common types of proxy servers in network communication:

Direction: Forward proxy is client-oriented, reverse proxy is server-oriented.
Location: The forward proxy is next to the client and the reverse proxy is next to the server.
Function: Forward proxy is used for client access control and anonymous access, and reverse proxy is used for load balancing and security.
Purpose: Forward proxy helps the client "forward" access to the Internet, and reverse proxy helps the server "reversely" handle external requests.

Set the domain name

To configure the domain name for your CDN server and make sure the domain name is correctly resolved to your server, you need to follow the steps below:

Step 1: Domain Name Registration and Purchase

  • Choose a domain name registrar, such as GoDaddy, Namecheap, Alibaba Cloud, etc.
  • Search for the domain name you want to register to make sure it is available.
  • Complete the purchase process and pay the corresponding fees.

Step 2: DNS Management

  • Log in to your domain registrar's control panel.
  • Find the section for DNS management or domain name management.

Step 3: Set up DNS records

  • A record: Create an A record to point the domain name to the public IP address of your CDN server. For example, if your CDN server IP is1.2.3.4, create an A record,Point to1.2.3.4
  • CNAME Records: If you need to redirect the subdomain to the CDN server, you can create a CNAME record. For example,Can CNAME to

Step 4: Configure the Web Server on the CDN Server

  • Make sure that your CDN server has web server software installed, such as Nginx or Apache.
  • Configure the web server so that it can provide services through your domain name. For example, in Nginx, you need to configure a server block to listen for port 80 (HTTP) or port 443 (HTTPS):
    server {
     	listen 80;
     	server_name ;
     	root /path/to/your/web/root;
     	index  ;
     	location / {
         	try_files $uri $uri/ =404;
    	 }
     }
    
  • If you use the SSL/TLS certificate, make sure that HTTPS is configured and listen to port 443.

Step 5: Deploy SSL/TLS Certificate (recommended)

  • If you want to enable HTTPS, you can get a free SSL/TLS certificate through Let’s Encrypt.
  • Install and configure the certificate to enable the web server to use SSL/TLS encryption.

Step 6: Test the domain name resolution

  • Waiting for the DNS changes to take effect globally, this can take some time (usually a few minutes to 48 hours).
  • Use command line tools such aspingornslookupCheck whether the domain name has pointed to the correct IP address.

Step 7: Verify the configuration

  • Enter your domain name in your web browser to see if you can successfully access the content on your CDN server.
  • Check the access log of the web server and confirm that there are access records from different regions.

Step 8: Monitoring and Maintenance

  • Regularly check the domain name's DNS records and the status of the CDN server to ensure that everything is running normally.

After completing the above steps, your domain name should correctly point to your CDN server, and users can access your website content through the domain name. Remember to double-check the syntax of the configuration file when making any changes and restart the web server after saving the changes.

Enable Gzip compression:

Enable Nginx's Gzip compression feature to reduce the size of the transmitted content and improve the loading speed of the website. You can add the following in the Nginx configuration file:

http {
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}

Set content source:

Configure Nginx to get content from your source server (usually your website server). You can forward the request to the origin server using the proxy_pass directive.

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g;
server {
    ...
    location / {
        proxy_pass http://backend;
        proxy_cache my_cache;
        proxy_cache_bypass $http_cache_control;
        ...
    }
}

Testing and Tuning:

After completing the above configuration, test your CDN server to make sure it works as expected. You may need to adjust the configuration to optimize performance based on actual conditions.

Common ways to tune CDN:

Tuning CDN (content distribution network) usually requires a comprehensive consideration of a variety of factors, including caching policy, node selection, network optimization, etc. Here are some specific tuning steps and examples:

1. Cache policy tuning:

Example: If your website has a large number of static resources such as images, CSS, and JavaScript files, you can increase the cache hit rate by setting a longer Cache-Control header value. For example, you can set it in Nginx:

 location ~* \.(jpg|jpeg|png|gif|js|css)$ {
         expires 30d;
     }

2. Node selection optimization:

Example: Use the CDN provider's geo-DNS or any cast DNS feature to direct users to the nearest server node, reducing latency. For example, in Alibaba Cloud CDN, DNS resolution policy can be configured to ensure users are accessing nearby.

3. Content warm-up:

Example: Before the new product is released, use the CDN content preheating function to distribute resources to each edge node in advance to avoid putting pressure on the source station during high traffic periods.

4. Dynamic content optimization:

Example: For frequently changing content, you can configure the CDN cache to a short expiration time or use the "no-cache" directive to ensure that the user gets the latest data.

5. HTTPS optimization:

Example: Enable SSL/TLS acceleration, such as using SPDY or HTTP/2 protocols, to reduce the HTTPS handshake time. At the same time, ensure that the CDN supports OCSP Stapling and reduce the SSL certificate verification time.

6. Network connection optimization:

Example: If `traceroute` shows that the user's routing path to the CDN node is not ideal, you can contact the CDN provider to optimize the routing policy.

7. Resource compression:

Example: Enable Gzip compression to reduce the size of the transmitted data. In Nginx you can set:

location ~* \.(html|css|js|json|xml|ld\+json|ejs|svg|png|jpg|jpeg|gif|ico)$ {
         gzip on;
     }

8. HTTP/2 enabled:

Example: Enable this protocol on a CDN node that supports HTTP/2, and use its multiplexing, server push and other features to improve performance.

9. Picture optimization:

Example: Use the CDN image processing function to automatically perform image compression and format conversion, such as converting JPG to WebP format.

10. Performance monitoring and log analysis:

Example: Check CDN logs regularly, analyze error codes, access patterns, and performance metrics, and use tools such as GoAccess or AWStats for log analysis.

11. Customized configuration:

Example: Customize the CDN configuration according to the website characteristics. For example, for video websites, a CDN can be configured to support smooth transmission of video streams.

12. Use performance testing tools:

Example: Use WebPageTest or Alibaba Cloud's cloud test products to regularly test CDN performance and analyze performance performance under different conditions.

13. Troubleshooting:

Example: If you find that some resources are loading slowly, check whether the resource URL is correctly cached by the CDN and confirm that there is no cache invalidation due to unexpected query strings.

14. User behavior analysis:

Example: Analyze user access patterns, identify popular content and high traffic periods, and adjust CDN strategies accordingly, such as increasing the cache of popular content or increasing bandwidth during peak periods.

Through the above tuning steps, the performance of the CDN can be significantly improved, website loading speed and user experience can be improved. Tuning is an ongoing process that requires regular performance testing and configuration adjustments.

For video websites, CDN can be configured to support smooth transmission of video streams.

For video websites, ensuring the smooth transmission of video content is the key to improving user experience. Here are some specific configuration steps and examples:

1. Caching strategy for video content:

Example: Configure the cache time of video files in Nginx, and use the `proxy_cache_valid` directive to set different cache times for different types of video content.

 location ~* \.(mp4|avi|mov)$ {
         proxy_cache my_cache;
         proxy_cache_valid 200 1h;
         proxy_cache_valid 404 1m;
     }

2. Video slicing and chunking transmission:

Example: Using Apple HLS (HTTP Live Streaming) or DASH (Dynamic Adaptive Streaming over HTTP) technology, the video is divided into multiple small clips, allowing the client to load different parts of the video on demand.

Configure CDN to support these protocols to ensure that clients can efficiently request video slicing from the nearest node.

3. Adaptive bit rate stream:

Example: Enable adaptive bit rate streaming on a video server, allowing the client to select the most appropriate video quality based on the current network conditions. This is usually achieved through HLS or DASH.

4. Content warm-up:

Example: For popular video content that is about to be released, use the CDN content warm-up function to distribute the video content to each edge node in advance to reduce the pressure on the source site during release.

5. Video encoding and compression:

Example: Use the FFmpeg tool to compress and transcode videos to generate video streams of different resolutions and bit rates to suit the bandwidth conditions of different users.

ffmpeg -i input.mp4 -c:v libx264 -crf 24 -preset veryfast -c:a aac -b:a 128k -movflags +faststart output.mp4

6. Enable HTTP/2:

Example: Enable HTTP/2 on the server, use its multiplexing feature to reduce the number of connections and improve video transmission efficiency.

     server {
         listen 80;
         listen 443 ssl http2;
         server_name ;
         ...
         http2_push_preload on;
     }

7. Load balancing and failover:

Example: Setting load balancing policies in a CDN configuration, such as polling, minimal connections, or IP hashing, ensure that video requests are evenly distributed to individual nodes. At the same time, configure the failover mechanism to automatically switch when the node fails.

8. Video player configuration:

Example: Use a player that supports adaptive bitrate streaming, such as or JW Player, and configure the player to get the video stream from the CDN node.

9. Monitoring and log analysis:

Example: Monitor video stream performance such as request volume, error rate and transmission speed using CDN provider monitoring tools such as log analysis by AWS CloudFront.

10. Bandwidth reservation and QoS:

Example: Leave enough bandwidth for video streams on the CDN and set priority to ensure that video transmission quality can be maintained during high traffic times.

11. Use the QUIC protocol:

Example: If the CDN supports the QUIC protocol, it can be enabled to reduce connection establishment time and improve the stability of video transmission.

12. Client adaptation:

Example: Automatic network conditions detection is implemented in a video player, and the most appropriate video quality is automatically selected based on the user's bandwidth and device performance.

Through the above configuration, the performance of the video website can be significantly improved, buffered and interrupted, and a smoother video viewing experience can be provided. These measures need to be adjusted and optimized according to the specific CDN provider and video website needs.

test

testCDN(Content distribution network)Performance usually involves the following steps:

1. **选择test工具**:
   - Use online tools such asWebPageTest、GTmetrixorGoogle PageSpeed Insights进行初步test。
   - example:accessWebPageTestwebsite,输入你ofwebsiteURL,并选择不同oftest位置and网络条件进行test。

2. **性能基准test**:
   - In implementing anyCDNBefore configuration changes,进行性能基准test,Easily compare and optimize the effect。
   - example:Record the initial loading time、TTFB(First byte time)、Key indicators such as cache hit rate。

3. **全球性能test**:
   - use全球分布式oftest工具,likeAkamai's CloudTestorGomez,test不同地理位置下of性能。
   - example:useCloudTestFrom North America、亚太and欧洲等多个地区testwebsiteof加载时间and响应时间。

4. **资源加载test**:
   - use工具likeApache JMeterorSiegeSimulate high concurrency requests,testCDNProcessing capability for resource loading。
   - example:existJMeter中Configuration多个线程组模拟用户并发access,testCDNLoad balancing and responsiveness。

5. **视频and流媒体test**:
   - like果website包含视频内容,use视频特定oftest工具,likeWiresharkorNagios,Monitor the stability and quality of video streams。
   - example:useWiresharkCapture packets during video streaming,Analyze latency and packet loss。

6. **SSL/TLStest**:
   - 对于useHTTPSofwebsite,useSSL LabsofSSL Test工具testCDN服务器ofSSL/TLSConfiguration。
   - example:输入websiteURL进行test,GetSSL/TLSConfigurationof评级and改进建议。

7. **缓存策略test**:
   - examineCDNIs the cache policy correctly implemented?,like验证`Cache-Control`and`Expires`head。
   - example:usecurl命令test资源ofHTTPhead信息:
     ```bash
     curl -I /
     ```

8. **DNS解析test**:
   - use`dig`or`nslookup`命令testCDNofDNS解析速度and准确性。
   - example:implement`dig `CheckDNS解析结果and时间。

9. **监控and日志分析**:
   - useCDN提供商of监控系统,分析实时性能数据and日志文件。
   - example:existAWS CloudFront中Check实时日志,Analyze the request volume、错误率and流量模式。

10. **用户体验test**:
    - useReal User Monitoring (RUM) 工具收集真实用户of性能反馈。
    - example:integratedNew RelicorGoogle AnalyticsofRUMFunction,追踪实际用户of性能体验。

11. **故障转移test**:
    - Simulate network failures,testCDNof故障转移机制是否有效。
    - example:use网络模拟器likeClumsyDisconnect,lookCDNCan I automatically switch to a healthy node。

12. **性能调优test**:
    - 根据test结果调整CDNConfiguration,like修改缓存时间、Load balancing strategies, etc.,然后重新test以验证改进效果。
    - example:existNginxAfter adjusting the cache policy,再次useWebPageTest进行test,Comparison of performance differences before and after。

Through the above steps,你可以全面地testCDNof性能,Identify bottlenecks,并根据test结果进行调优。remember,CDNtestand调优是一个持续of过程,需要定期进行以适应website流量and内容of变化。

Multiplexing

Multiplexing is a communication method that allows multiple data streams to share a single physical link and distinguish different data streams by different identifiers. In network protocols, multiplexing counts generally refer to the number of independent data streams that can be transmitted simultaneously in the context of multiplexing.

In the HTTP/2 protocol, multiplexing is a core feature that allows clients and servers to send multiple requests and responses simultaneously on a single TCP connection, thereby reducing the overhead of connection establishment and closing and improving transmission efficiency.

Some key points of multiplexing counting:

  • Parallel transmission: In HTTP/2, multiplexing allows multiple requests and responses to be transmitted in parallel on the same TCP connection, rather than requiring one connection to be established and closed for each request/response like HTTP/.

  • Stream ID: Each HTTP/2 request or response stream is assigned a unique stream ID to distinguish different data streams.

  • Performance improvement: Multiplexing reduces the connection establishment and closing time, reduces the redundant transmission of HTTP header information, thereby improving the loading speed of web pages.

  • Stream priority: HTTP/2 supports stream priority and weight, allowing clients and servers to control the transmission order of different streams.

  • Head compression: The HPACK algorithm is used to compress HTTP headers, further reducing the amount of data transmission.

  • Flow control: HTTP/2 uses a flow control window to control the data transmission rate to avoid network congestion.

example:

Suppose a web page needs to load multiple resources, such as HTML, CSS, JavaScript files and images. In HTTP/, these resources may need to be loaded sequentially over multiple TCP connections. In HTTP/2, all of these resources can be loaded on a single TCP connection at the same time, each resource is assigned a stream ID, through which the client and server distinguish and manage these parallel transport streams.

For example, the client may send the following HTTP/2 request:

HEADERS (stream_id=1): :method: GET
                   :scheme: https
                   :path: /css/
                  :authority: 
                 :priority: 1
BLOCK (stream_id=1): [data for stream 1]
HEADERS (stream_id=3): :method: GET
                   :scheme: https
                   :path: /js/
                  :authority: 
                 :priority: 3
BLOCK (stream_id=3): [data for stream 3]

In this example, stream 1 and stream 3 are two independent requests that are distinguished by different stream IDs and can be transmitted on a single TCP connection at the same time.

In general, multiplexing count refers to the number of streams that can be opened in a multiplexed connection, and it is an indicator of the multiplexing capability. In HTTP/2, multiplexing allows for more efficient data transmission, reducing web page loading time and improving user experience.

This is the article about how to build your own CDN server in Nginx. For more information about Nginx, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!