Advanced routing using Fastly CDN

Author Photo
Alexandr Tihanov
Senior Developer at comwrap

Advanced routing using fastly CDN

Fastly is a Varnish-based service used in Adobe Commerce Cloud (Magento) for content delivery, security, and image optimization. In this article, I describe some of the advanced routing features for developers.

The usual way to manage routing in Adobe Commerce Cloud is to use routes.yaml facilities. These rules are then propagated into the Nginx settings.

This is sufficient as long as you have simple routing requirements. However, if you have more complex requirements, using Fastly is a better choice. It allows you to create complex routing at a higher level before the request reaches Magento.

Backend Settings

Also «Backend» could be called «Origin Server». For instance, we have two systems. The old and new one (Adobe Commerce Cloud). The client implemented new features and moved part of the catalog to the new system, but without changing the domain name. He has multiple marketplaces and still needs to use the old system for several cases. These cases relate to URLs and can be implemented as a regular expression.

By default Fastly picks content from the Adobe server. But in «Backend Settings» the developer can set the rule and put the IP of another server, which will be used as the origin for picking up the content. A detailed explanation of how to implement this functionality is already described here, using 3rd party CMS integration as an example.

Non-regex Redirects

Fastly does not have a straight mechanism for setting a list of redirects. To implement this functionality, the developer should perform several steps.

The first - is a dictionary creation. Dictionary has to contain a list of redirects as source (key) and target (value).

Dictionary can be created in Stores → Configuration → Advanced → System → Full Page Cache → Fastly Configuration -> Edge Dictionaries. In the first step of the dictionary creation, the «Name» has to be set. It will be used further for dictionary binding. For this article, I use «redirects1» as a name.

Example of Edge dictionary creation

The next step is to create custom snippets. Open Stores → Configuration → Advanced → System → Full Page Cache -> Fastly Configuration → Custom VCL Snippets, and create two snippets.

First, one is needed to find the appropriated entity in the dictionary and return an error with the code, which will be processed in the second snippet. It is not necessary to use the exact 912 error code number. The code is needed to identify the redirect path in the error processing snippet.

“reqv” custom snippet creation

As you can see on the screenshot, there is an additional term set in the snippet. This is so to avoid wrong host redirects in the author`s case when Magento contains multiple stores on different domains.

The second snippet processes the error code and returns a response with the passed URL.

“error” custom snippet creation

Once VCL is updated, redirects start to work. Put attention that request is managed on Fastly level. So routes.yaml and Magento backend redirects are not touched because the request does not touch the Magento environment.

Dictionaries Managing

For the previous example, I used the «Edge Dictionaries» functionality to create redirects. For the list of several rows that is enough to use manual editing. But once you have a list of several thousand redirects, that will be a long way. To simplify dictionary creation, Magento provides import functionality. It is located on Stores → Configuration → Advanced → System → Full Page Cache → Fastly Configuration → Tools → Import. To import data, you need to save it in a file in JSON format. Choose this file and click «Import». The file will be uploaded to the server and in the next window, you will see the structure of the contained data. Choose the needed kind of data and click «Import» again.

The good question is «How can I get the correct file structure». From my perspective, the easiest way is to export data before importing, using the same menu. Then modify it in a text editor.

Be aware, that each Edge dictionary is limited to a maximum of 1000 rows. If it is more, Magento imports only the first 1000.

Once I faced a list of around 7 thousand redirects from an SEO company. To overcome this task, I have got an idea that I would like to share. The main idea is to split data into several dictionaries per 1000 rows in each and check each dictionary in «Custom Snippet». Php script and text editor were used to quickly gather data in the needed format. So I had dictionaries with names like «redirects1», «redirects2», «redirects3», etc. Then snipped was created with such code.

Using multiple dictionaries in Fastly

So, as you can see, all dictionaries are checked for the key which should be redirected. And only an error code is used. Further processing is the same as provided in the previous example.

The goal of this article was to broadcast a vision of Fastly possibilities besides only the CDN system.

Photo by Sylvain Gllm on Unsplash

Related Articles

  • 2022-04-22
    Disaster Recovery in an Adobe Commerce Cloud environment
    Author Photo
    Alex Lyzun
    Lead Developer Adobe Commerce
    Adobe Commerce Cloud has an automated backup process by design. So, if you act quickly after a notice, you have a good chance to restore your system almost completely.
  • 2021-12-30
    Overcoming Adobe Commerce Cloud's sales increment ID step limitation
    Author Photo
    Vasyl Malanka
    Senior Developer at comwrap
    Quick guide on how to tackle Magento Commerce Cloud's auto-increment setting of three for unique IDs