Set up Nginx with Brotli Compression on Debian 12 Bookworm | Easy Steps

Posted on

Set up Nginx with Brotli Compression on Debian 12 Bookworm | Easy Steps

Set up Nginx with Brotli Compression on Debian 12 Bookworm | Easy Steps

In this comprehensive guide, we will walk you through the process of Set up Nginx with Brotli Compression on Debian 12 Bookworm. Brotli is a superior compression algorithm designed to significantly reduce file sizes, leading to faster page loading times and an improved user experience. Follow these detailed instructions on the Orcacore website to Set up Nginx with Brotli Compression on Debian 12 Bookworm.

Before you begin, ensure you have SSH access to your Debian 12 server as a root user. If you require assistance with the initial server setup, refer to our guide on Initial Server Setup with Debian 12 Bookworm.

Let’s dive into the steps required to Set up Nginx with Brotli Compression on Debian 12 Bookworm.

Step 1 – Install Required Packages for Nginx Brotli

First, update your system’s package list to ensure you have the latest versions available:

sudo apt update

Next, install the necessary packages and dependencies required for building Nginx with Brotli support:

apt install git dpkg-dev curl gnupg2 build-essential zlib1g-dev libpcre3 libpcre3-dev unzip -y

Step 2 – Add Nginx Repository on Debian 12 Bookworm

To ensure you have access to the latest Nginx packages, you need to add the official Nginx repository to your Debian 12 server.

Begin by adding the Nginx signing key using the following curl command:

curl -L https://nginx.org/keys/nginx_signing.key | apt-key add -

Then, create a new file for the Nginx repository configuration using your preferred text editor (we’ll use vi here):

vi /etc/apt/sources.list.d/nginx.list

Add the following lines to the file:

deb http://nginx.org/packages/debian/ bookworm nginx
deb-src http://nginx.org/packages/debian/ bookworm nginx

Save and close the file.

Update your system’s package list again to include the newly added repository:

apt update

Step 3 – Download Nginx and Brotli Source on Debian 12

Navigate to the /usr/local/src directory, which is a common location for storing source code:

cd /usr/local/src

Download the Nginx source code using the apt source command:

apt source nginx

Install the build dependencies required for compiling Nginx:

apt build-dep nginx -y

Clone the latest version of the Brotli source code from the GitHub repository:

git clone --recursive https://github.com/google/ngx_brotli.git

Change the directory to the Nginx source code directory:

cd /usr/local/src/nginx-*/

Edit the debian/rules file using your favorite text editor (again, we’re using vi):

vi debian/rules

Locate the config.env.nginx and config.env.nginx_debug build environments. Add the --add-module=/usr/local/src/ngx_brotli option to both environments:

--add-module=/usr/local/src/ngx_brotli

Save and close the file.

Step 4 – Compile and Build Nginx With ngx_brotli Support on Debian 12

Compile and build the Nginx package with Brotli support using the following command:

dpkg-buildpackage -b -uc -us

This process may take a considerable amount of time to complete.

Once the build process is finished, you will find the nginx-*.deb packages in the /usr/local/src directory.

Verify the presence of the .deb packages using the following command:

ls -l /usr/local/src/*.deb

Your output should resemble the following:

Compile and Build Nginx With ngx_brotli Support on Debian 12

Step 5 – Install Nginx With Brotli on Debian 12

Navigate back to the /usr/local/src directory:

cd /usr/local/src/

Install the newly built Nginx packages using the dpkg -i command:

dpkg -i *.deb

Step 6 – Enable Brotli Support on Debian 12

Open the main Nginx configuration file for editing:

vi /etc/nginx/nginx.conf

Add the following lines within the http { ... } block to enable Brotli compression:

    brotli on;
    brotli_comp_level 6;
    brotli_static on;
    brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;

Save and close the file.

Verify the Nginx configuration for any syntax errors:

nginx -t
**Output**
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Nginx service to apply the changes:

systemctl restart nginx

Step 7 – Test Nginx Brotli Compression Support

To confirm that Brotli compression is enabled and working correctly, use the following curl command:

curl -H 'Accept-Encoding: br' -I http://localhost

If Brotli is enabled, you should see content-encoding: br in the output:

**Output**
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sun, 06 Aug 2023 09:21:36 GMT
Content-Type: text/html
Last-Modified: Tue, 11 Apr 2023 01:45:34 GMT
Connection: keep-alive
ETag: W/"6434bbbe-267"
**Content-Encoding: br**

Congratulations! You have successfully Set up Nginx with Brotli Compression on Debian 12 Bookworm.

Conclusion

Brotli compression is a highly effective technique for improving website performance. By following the steps outlined in this guide, you have successfully learned how to Set up Nginx with Brotli Compression on Debian 12 Bookworm.

Now, let’s explore alternative methods for achieving the same result:

Alternative Solutions for Enabling Brotli Compression in Nginx

While the previous method involves compiling Nginx from source, there are alternative approaches that can simplify the process, especially when using pre-built packages.

Alternative 1: Using the nginx-extras Package (If Available)

Some distributions provide an nginx-extras package that includes Brotli support as a module. This is often the easiest method if available.

  1. Check for the nginx-extras package:

    apt search nginx-extras
  2. Install nginx-extras:

    If the package is available, install it:

    sudo apt install nginx-extras
  3. Enable the Brotli module:

    The Brotli module might be disabled by default. You may need to create a symbolic link to enable it. The exact commands may vary based on the distribution. Look for files in /etc/nginx/modules-available/ and /etc/nginx/modules-enabled/. If you see brotli.conf in modules-available, create a symbolic link:

    sudo ln -s /etc/nginx/modules-available/brotli.conf /etc/nginx/modules-enabled/
  4. Configure Brotli:

    Add the Brotli configuration to your nginx.conf file, as shown in Step 6 of the original instructions.

  5. Test and Restart Nginx:

    Verify the configuration and restart Nginx:

    nginx -t
    sudo systemctl restart nginx

    Test with curl as shown in Step 7.

Explanation:

This method leverages pre-built packages to provide Brotli support, eliminating the need for manual compilation. The nginx-extras package often includes a variety of additional modules, making it a convenient option. However, the availability and specific configuration steps can vary depending on your Debian 12 setup.

Alternative 2: Using Dynamic Modules (If Supported by Your Nginx Build)

Modern versions of Nginx often support dynamic modules, allowing you to load modules without recompiling the entire server. This approach involves downloading a pre-compiled Brotli module and loading it dynamically.

  1. Download a pre-compiled Brotli module:

    Finding a pre-compiled module that matches your Nginx version and system architecture can be challenging. A good starting point is to search online for "nginx brotli dynamic module debian 12". Ensure the source is trustworthy before downloading and installing. There are often community-maintained repositories that provide these. If you find a suitable module, download the .deb package. For example (replace with the actual filename):

    wget https://example.com/nginx-module-brotli_1.24.0-1_amd64.deb
  2. Install the module:

    sudo dpkg -i nginx-module-brotli_1.24.0-1_amd64.deb

    You might need to run sudo apt --fix-broken install if there are dependency issues.

  3. Load the module in nginx.conf:

    Add the following line at the top of your nginx.conf file, outside the http block:

    load_module modules/ngx_http_brotli_filter_module.so;
    load_module modules/ngx_http_brotli_static_module.so;

    The exact path to the .so file may vary depending on where the module was installed. Check the output of the dpkg command or look in /usr/lib/nginx/modules/.

  4. Configure Brotli:

    Add the Brotli configuration to your nginx.conf file, as shown in Step 6 of the original instructions.

  5. Test and Restart Nginx:

    Verify the configuration and restart Nginx:

    nginx -t
    sudo systemctl restart nginx

    Test with curl as shown in Step 7.

Explanation:

Dynamic modules provide a more convenient way to extend Nginx functionality without requiring recompilation. This method relies on finding a compatible pre-compiled Brotli module for your specific Nginx version and operating system. It is critical to find a module that is compatible with your exact Nginx version; otherwise, Nginx will likely fail to start. Be especially cautious when downloading pre-compiled modules from untrusted sources.

Both alternative methods provide easier paths to enabling Brotli compression compared to compiling from source. However, they rely on the availability of pre-built packages or modules and require careful attention to compatibility and security. Choose the method that best suits your specific needs and environment. The most important thing is to regularly update your server and its components to maintain optimal performance and security.

Leave a Reply

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