CDN multilingual caching

更新时间:
复制 MD 格式

Use CDN multilingual caching to deliver language-specific versions of your content based on the client Accept-Language request header. This topic describes how to configure an Nginx origin server to serve different language versions of a resource and leverage CDN caching to serve each version from the edge.

Overview

When your website serves users in multiple regions, you may need to deliver content in different languages. CDN multilingual caching allows the CDN edge nodes to cache different language versions of the same URL separately. When a user requests a resource, the CDN forwards the Accept-Language header to the origin server, which returns the appropriate language version. The CDN then caches that version and serves subsequent requests for the same language from the edge.

The process works as follows:

  1. A user sends a request to the CDN edge nodes with an Accept-Language header (for example, zh or en).

  2. The CDN edge nodes forward the request to your origin server, including the Accept-Language header.

  3. The origin server parses the Accept-Language header and returns the corresponding language version of the resource.

  4. The CDN caches the response, keyed by the Accept-Language header. Subsequent requests for the same language version are served directly from the edge.

Prerequisites

Before you configure multilingual caching, ensure that you have:

  • A domain name that is accelerated by CDN

  • An Nginx origin server with language-specific resource files prepared

Configure the origin server

Prepare the language-specific versions of your files on the origin server. For this example with Chinese and English versions, create two files named index-cn.html and index-en.html in the Nginx service directory. You do not need to create an index.html file.

Add conditional logic to the Nginx configuration file. The CDN multilingual caching feature relies on responses from the origin server. Therefore, you must implement the logic for serving different language versions on your origin server. The following configuration demonstrates a complete setup:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_accept_language"';
    access_log /var/log/nginx/access.log main;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Match language preference based on the Accept-Language header from the client request
    map $http_accept_language $preferred_lang {
        default "cn";
        ~*\ben\b "en";  # Exact match for en
        ~*\bzh\b "cn";  # Exact match for zh
        ~*en "en";      # Fuzzy match for en
        ~*zh "cn";      # Fuzzy match for zh
    }
    server {
        listen 80;
        server_name xxxxxxxxxxxx;  # Replace with your actual domain name
        root /var/www/html;  # Replace with your actual document root
        charset utf-8;

        # Rewrite all requests for / to /index.html to trigger language selection
        location = / {
            return 302 /index.html;
        }

        # Core: Handle requests for /index.html
        location = /index.html {
            # Select the file based on the map result
            set $lang_file "index-${preferred_lang}.html";
            try_files /$lang_file =404;
            add_header Vary "Accept-Language" always;
        }

        # Other static resources
        location / {
            try_files $uri $uri/ =404;
        }
    }
}
Note

Key configuration blocks explained:

  • map $http_accept_language $preferred_lang: Maps the Accept-Language request header to a language preference variable. The configuration uses both exact (\ben\b) and fuzzy (~*en) matching for reliability.

  • location = /index.html: Based on the matched language, serves the corresponding language-specific file (for example, index-en.html or index-cn.html) and adds the Vary: Accept-Language response header. This header instructs the CDN to cache different versions of the page separately.

Configure the CDN

No special configuration is required for the CDN. You only need to set up a standard cache TTL rule.

  1. Log on to the CDN console. In the left-side navigation pane, click Domain Names. On the domain management page, find the target domain and click Manage in the Actions column.

  2. In the left-side navigation pane, choose Cache > Cache TTL.

  3. Click Add to configure a cache expiration time. In the dialog box, set the following parameters:

    Configure the following parameters:

    Type

    Value

    Type

    File Extension

    File extension

    html

    Expiration time

    10 days

Error handling

  • Missing Accept-Language header: If the client request does not include the Accept-Language header, the origin server returns the default language version configured in the map block (in this example, the Chinese version).

  • Origin 404 response: If the origin server does not have the requested language-specific file, it returns a 404 error. The CDN caches the 404 response for the duration specified in the cache TTL rule. To avoid caching 404 responses, configure a separate cache rule for 4xx status codes.

  • Missing Vary header: If the origin response does not include the Vary: Accept-Language header, the CDN caches only the first requested version. Subsequent requests from users with different language preferences receive the cached version, resulting in incorrect content delivery.

Verification

Use the curl command to test the configuration and verify that the CDN correctly caches and serves language-specific content.

  1. Request the Chinese version:

    curl -H "Accept-Language: zh,zh;q=0.9" http://yourdomain.com/index.html -v

    Expected output: The response body contains the Chinese version of the page (index-cn.html), and the response header includes Vary: Accept-Language.

  2. Request the English version:

    curl -H "Accept-Language: en,en;q=0.9" http://yourdomain.com/index.html -v

    Expected output: The response body contains the English version of the page (index-en.html), and the response header includes Vary: Accept-Language.