Bobcares

Optimizing Web Performance with Brotli Compression in NGINX Ingress

by | Dec 21, 2024

To optimize web performance with Brotli Compression in NGINX Ingress, read our latest blog. At Bobcares, we assist our customers with several queries on a daily basis as part of our Kubernetes Support.

Overview
  1. Understanding Brotli Compression in NGINX Ingress
  2. Configuring Brotli in NGINX Ingress
  3. Best Practices for Brotli Compression
  4. Conclusion

Understanding Brotli Compression in NGINX Ingress

When it comes to web application performance, efficient data compression is a game-changer. While gzip has long been the go-to compression method, Brotli is increasingly recognized for its superior compression efficiency and faster delivery times. In this guide, we explore how to enable Brotli compression in NGINX Ingress for the Kubernetes cluster. Developed by Google, Brotli is a high-performance compression algorithm designed for web assets. It compresses files more effectively than gzip, particularly for text-based assets like HTML, CSS, and JavaScript. The benefits include:

nginx ingress brotli

  • Smaller File Sizes: Brotli can reduce file sizes up to 20-30% more than gzip.
  • Faster Load Times: Reduced file sizes mean quicker data transfer and improved user experience.
  • Efficient Bandwidth Usage: Brotli optimizes data transmission, saving server and client bandwidth.

By enabling Brotli in NGINX Ingress, we can leverage these advantages for applications running in Kubernetes.

Configuring Brotli in NGINX Ingress

In NGINX Ingress, configurations are managed using a ConfigMap. Here’s a step-by-step guide to enabling and optimizing Brotli compression.

Step 1: Locate the ConfigMap

First, find the ConfigMap associated with the ingress-nginx deployment. Run the following command to list ConfigMaps across all namespaces:

kubectl get cm -l app.kubernetes.io/name=ingress-nginx -A

The output will look something like this:

NAMESPACE NAME DATA AGE

my-namespace ingress-nginx-configuration 0 14d

Identify the ingress-nginx-configuration ConfigMap in the namespace.

Step 2: Create or Modify the ConfigMap

View Current Configuration

To inspect the existing configuration, use:

kubectl get cm ingress-nginx-configuration -n my-namespace -o yaml

Create a ConfigMap YAML File

Instead of editing directly, create a new file configmap.yaml with the following content:

apiVersion: v1
kind: ConfigMap
metadata:
name: ingress-nginx-configuration
data:
enable-brotli: "true"
brotli-level: "6"
brotli-types: "text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap"

Key Parameters:

  • enable-brotli: Enables Brotli compression (true or false).
  • brotli-level: Sets the compression level (1-11, default is 4). Higher values yield better compression but require more CPU resources.
  • brotli-types: Specifies the MIME types to compress (e.g., text/css, application/json).
Step 3: Apply the ConfigMap

Apply the ConfigMap to the same namespace as the ingress-nginx deployment:

kubectl apply -f configmap.yaml -n my-namespace
Step 4: Verify Configuration

Check if the configuration was applied successfully:

kubectl get cm ingress-nginx-configuration -n my-namespace -o yaml

Ensure the output reflects the updated Brotli settings:

apiVersion: v1
data:
enable-brotli: "true"
brotli-level: "6"
brotli-types: text/xml image/svg+xml application/x-font-ttf application/json text/javascript text/css
kind: ConfigMap
Step 5: Test Brotli Compression

To confirm Brotli is working:

Make a request to the application using a browser or tools like cURL:

curl -H "Accept-Encoding: br" -I http://your-domain.com

Check the response headers for:

content-encoding: br

This confirms that Brotli compression is active.

Best Practices for Brotli Compression

  • Set Compression Levels Wisely

Use a moderate brotli-level (4-6) for a balance between performance and resource usage.

  • Prioritize MIME Types

Include frequently accessed MIME types, such as application/javascript and text/css.

  • Monitor Performance

Track CPU and memory usage after enabling Brotli to ensure it does not overwhelm the infrastructure.

  • Combine with gzip

Enable both Brotli and gzip to ensure compatibility with older clients.

[Need to know more? Get in touch with us if you have any further inquiries.]

Conclusion

Enabling Brotli compression in NGINX Ingress is a straightforward yet powerful way to enhance web application performance. By following the steps outlined above, we can deliver faster load times, reduce bandwidth usage, and provide a superior user experience. Brotli isn’t just a feature—it’s a performance boost the application can’t afford to miss!

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF