CDNs can improve the performance of static websites. Within the context of a global training business, your content needs to be easily accessible and up-to-date. In this unit, you’ll learn how to make other configuration changes so that CDNs work properly with static sites in production environments.
Last updated: Feb 12, 2023
Using Custom domains
From the exercise, you saw how the CDN had a unique URL in the form of endpointname.azureedge.net. Regardless of where the user is in the world, this URL will point to the nearest POP, in that way improving performance. While this approach works fine, the URL isn’t memorable, and doesn’t reflect your company’s brand.
Custom domain settings enable you to specify a Canonical Name (CNAME) record in Domain Name System (DNS) that points to the CDN URL. Suppose the user types in the custom domain name, for example www.contoso.com. The DNS maps that domain name to the POP endpoint URL and connects the user to that URL.
Create a CNAME DNS record
To provide custom domain mapping, you first need to create the CNAME record in DNS. How you do the mapping depends on the interface that your DNS provider implements. However, what you need to configure is a record in the following form:
|Address (left field)||type||Points to (Right field)|
DNS may take up to 72 hours to update.
Mapping the temporary cdnverify subdomain
You can configure the CNAME record and publish it if your web server isn’t yet online. If your domain is already in production and pointing to the origin server, you don’t want to have any interruption to your users when you configure the CDN record. CNAME records should point to the cdnverify subdomain. In this case, the record will look like this format:
|Address (left field)||type||Points to (Right field)|
Typically, you would configure this record with a Time to Live (TTL) of 1 hour.
When you’ve configured the cdnverify subdomain and the CNAME for the domain mapping, you can now add the custom domain.
Adding a custom domain
To add a custom domain, go to the CDN endpoint that you created, and under Settings, select Custom domains. In the Custom domain pane, select Custom domain, then in the Add a custom domain, under Custom hostname, enter the hostname that matches the CNAME record in your custom domain, such as www.contoso.com.
When you enter the custom domain name, Azure will use DNS to attempt to resolve the address to the endpoint hostname. You’ll see a tick next to the **Custom hostname field if they match. If you see a red exclamation mark, then you should check your DNS settings.
If the custom domain name resolves to the endpoint hostname, select Add. Now any users going to www.contoso.com will be redirected to the Azure POP nearest their location.
Azure CDN can improve performance by compressing the files before they’re delivered. Files are then decompressed by the receiving browser. How this activity applies depends on whether the file is originally compressed on the origin server or not.
Azure CDN passes along the compressed files unaltered if you enable compression on files hosted on your origin server. Azure CDN dynamically compresses uncompressed files on the origin server that is of a type that can be compressed. It then stores the compressed files on the POP. This process improves the client experience and site performance.
Compression in Azure CDN Standard from Microsoft is on by default. You can’t configure additional file types to compress or delete existing file types. However, you can add and modify file types to compress in the Akamai Standard and Verizon profiles.
Controlling caching behavior
Azure CDNs provide two mechanisms for caching files. However, these configuration settings depend on the tier you’ve selected. Caching rules in Azure CDN Standard for Microsoft are set at the endpoint level and provide three configuration options. Other tiers provide additional configuration options, which include:
- Caching rules. Caching rules can be either global (apply to all content from a specified endpoint) or custom. Custom rules apply to specific paths and file extensions.
- Query string caching. Query string caching enables you to configure how Azure CDN responds to a query string. Query string caching has no effect on files that can’t be cached.
With the Azure CDN Standard for Microsoft Tier, caching rules are as simple as the following three options:
- Ignore query strings. This option is the default mode. A CDN POP simply passes the request and any query strings directly to the origin server on the first request and caches the asset. New requests for the same asset will ignore any query strings until the TTL expires.
- Bypass caching for query strings. Each query request from the client is passed directly to the origin server with no caching.
- Cache every unique URL. Every time a requesting client generates a unique URL, that URL is passed back to the origin server and the response cached with its own TTL. This final method is inefficient where each request is a unique URL, as the cache-hit ratio becomes low.
To change these settings, in the Endpoint pane, select Caching rules and then select the caching option that you want to apply to the endpoint and select Save.
Caching and time to live
If you publish a website through Azure CDN, the files on that site are cached until their TTL expires. The Cache-Control header contained in the HTTP response from origin server determines the TTL duration.
If you don’t set a TTL on a file, Azure CDN sets a default value. However, this default may be overridden if you have set up caching rules in Azure. Default TTL values are as follows:
- Generalized web delivery optimizations: seven days
- Large file optimizations: one day
- Media streaming optimizations: one year
For more information on caching, see the Further Reading section of the Summary unit.
In normal operation, an Azure CDN edge node will serve an asset until its TTL expires. The edge node reconnects to the origin server when the TTL expires and a client makes a request to the same asset. The node will fetch another copy of the asset, resetting the TTL in the process.
To ensure that users always receive the latest version of an asset, consider including a version string in the asset URL. This approach causes the CDN to retrieve the new asset immediately.
Alternatively, you can purge cached content from the edge nodes, which refreshes the content on the next client request. You might purge cached content when publishing a new version of a web app or to replace any out-of-date assets.
You can purge content in several ways.
- On an endpoint by endpoint basis, or all endpoints simultaneously should you want to update everything on your CDN at once.
- Specify a file, by including the path to that file or all assets on the selected endpoint by checking the Purge All checkbox.
- Based on wildcards (*) or using the root (/).
When you’ve specified what content you want to purge, select the Purge button.
For more information content expiration, see the Further Reading section of the Summary unit.
Geo-filtering enables you to allow or block content in specific countries, based on the country code. In the Azure CDN Standard for Microsoft Tier, you can only allow or block the entire site. With the Verizon and Akamai tiers, you can also set up restrictions on directory paths. For more information, see the further reading section in the Summary unit.
To configure geo-filtering, in the properties of the respective endpoint, select Geo-filtering. On the Geo-filtering panel, select either allow or block, then in the Country codes list, select which countries you want to allow or block.
The Allow setting is more restrictive than Block. Allow allows access only for the selected countries. The logic for Block is to allow access from all countries, except for those countries blocked.
For more information on CDN & good web hosting service, see the link Reading section in the Summary.