Optimizing your Magento 2 Cloud Commerce website’s performance and security has become crucial. Leveraging Fastly’s powerful content delivery network (CDN) capabilities, you can enhance your site’s speed and reliability, and also implement sophisticated access control mechanisms based on geographical location. 

This comprehensive guide will walk you through Magento 2: Fastly VCL Snippets & Country Access Control i.e., creating custom VCL (Varnish Configuration Language) snippets and setting up Access Control Lists (ACLs) in Fastly to fine-tune your site’s behavior and restrict access based on country.

Prerequisites

Let’s make sure of the following prerequisites before looking into the setup process::

  • Your Magento 2 Cloud Commerce environment is configured to use Fastly CDN.
  • You are running the latest version of the Fastly CDN module for Magento 2.
  • You have verified the Fastly service configuration for your environment.
  • You have Admin credentials to access the Staging and Production environments.

Read Also: Latest on Magento 2.4.7 Release [2024 Updates]

Adding Custom VCL Snippets

Via Magento Admin

  1. Navigate to the Configuration Section: Go to Stores > Settings > Configuration > Advanced > System.
  2. Expand Fastly Configuration: Find and expand the Full Page Cache > Fastly Configuration > Custom VCL Snippets.
  3. Add or Edit a Snippet: In the Action column, click the settings icon next to the snippet you wish to edit. If adding a new snippet, provide details such as the snippet name, type, priority, and the VCL code itself.
  4. Upload VCL to Fastly: After editing or adding your snippet, click Upload VCL to Fastly in the Fastly Configuration section. This action uploads your custom VCL to Fastly, applying the changes without server downtime.
  5. Refresh the Cache: Once the upload completes, refresh the cache according to the notification at the top of the page to ensure your changes take effect immediately.

Via Fastly API

Use the Fastly API for automating the process or direct interaction with the Fastly service configuration.

Example: Bypassing Fastly for Specific IP Addresses or URL

# Bypass Fastly for a specific IP address
if (client.ip == "<Your IPv4 IP address>" || client.ip == "<Your IPv6 IP address>") {
    return(pass);
}


# Bypass Fastly for a specific URL pattern
if (req.url ~ "/media/feeds/GoogleShoppingHiVisNew.xml") {  
    return(pass);
}

Replace <Your IPv4 IP address> and <Your IPv6 IP address> with the actual IP addresses you wish to bypass Fastly for. Use the == operator for an exact URL match.

Recommended Read: Magento Plugin Development

Restricting Website Access by Country

Step 1: Setting Up Fastly for Magento 2 Cloud Commerce

  • Log In to Your Fastly Account: Navigate to the Fastly dashboard and login with your credentials. Select the appropriate service associated with your Magento 2 Cloud Commerce site.
  • Configure Your Service: From the service menu, proceed to the Configuration section and click on the Edit Configuration button.

Step 2: Creating an Access Control List (ACL)

  • Add a New ACL: Within the VCL editor, locate the ACLs section and click on the + icon to add a new ACL. Assign a descriptive name and set the type to “Country.”
  • Define Your Conditions: Use the “Add a condition” button to specify the countries you wish to block. Optionally, use the “Negate” checkbox to exclude certain countries.
  • Save Your ACL: After defining your conditions, save the ACL.

Step 3: Implementing the ACL Check

  • Create a VCL Snippet: Navigate to the VCL Snippets section and click on the + icon to add a new snippet. Name your snippet appropriately.
  • Insert the ACL Check Code: In the VCL editor, insert the following code snippet:
if ( client.geo.country_code == "IN" ) {
if ( !(client.ip ~ allowlist) && !req.http.Fastly-FF) { error 403 "Forbidden"; }
}

Replace “name_of_your_acl” with the actual name of the ACL you created.

  • Save and Apply the Snippet: Save your snippet to make it available for use across your domains.

Step 4: Applying the Snippet to Your Domain

  • Edit Domain Settings: Go to the Domains section within your Fastly service and click on the Edit button next to the domain you wish to restrict access to.
  • Include the Snippet: Add the snippet you created in the VCL Snippets in this domain field.
  • Activate Your Configuration: Review your changes and activate the updated configuration to enforce the country-based access control.

Conclusion

By following the above steps, you’ve successfully implemented custom VCL snippets and country-based access control for your Magento 2 Cloud Commerce site using Fastly. This approach enhances your site’s security and performance and also allows you to tailor your online presence to your target market, optimizing user experience and potentially boosting sales. Remember, the flexibility of Fastly’s ACLs means you can adjust your restrictions as needed, adapting to changes in your business strategy or market dynamics.

Muhammed Salim

Muhammed Salim, a seasoned Senior Software Engineer at Klizer (DCKAP) and a certified Adobe Commerce Developer, brings an extensive background spanning over 6 years in the field, along with a wealth of experience. His commitment to continuous learning allows him to stay at the forefront of industry advancements, ensuring that he remains well-versed in the latest technologies and best practices within the Adobe Commerce ecosystem. Dedicated to creating exceptional solutions, Muhammed's passion for innovation drives him to exceed expectations and push the boundaries in Adobe Commerce development.

Get in Touch

Get Expert Advice

At Klizer, we bring over 18 years of specialized expertise in ecommerce website development and design.

© Copyright 2024 Klizer. All Rights Reserved