Boost Website Performance with Text Compression

When it comes to website performance, every second counts. One of the most effective ways to improve website speed and performance is by enabling text compression on your server. Text compression is a technique for reducing the amount of data that needs to be transferred over the network between the server and the client. This can greatly improve the speed and performance of a website.

There are several ways to enable text compression on your server, including Apache mod_deflate, Nginx gzip and htaccess.

Apache Mod

Apache mod_deflate is an Apache module that can be used to enable text compression on an Apache web server. To enable mod_deflate, you will need to add the following lines to your Apache configuration file:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

Nginx

Nginx also has a built-in module for text compression called gzip. To enable gzip, you will need to add the following lines to your Nginx configuration file:

gzip on;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  • The gzip_comp_level directive controls the compression level (1-9, with 9 being the highest level of compression).
  • The gzip_min_length directive controls the minimum length of content that will be compressed.

.htaccess

You can also enable compression on your server by adding the following code to your .htaccess file

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript
</IfModule>

nginx config

In Nginx, you will need to add the gzip configuration lines in the server block of your Nginx configuration file. The server block is where you configure how your server handles incoming requests. The location of the Nginx configuration file depends on your operating system and installation method.

  • On Ubuntu and Debian systems, the default location of the Nginx configuration file is usually /etc/nginx/nginx.conf.
  • On other systems, it may be located in /etc/nginx/conf.d/default.conf or in another location specific to your installation.

Here is an example of where the gzip configuration lines could be added in the server block of the nginx.conf file:

http {
    ...
    gzip on;
    gzip_comp_level 6;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    ...
    server {
        ...
        # other server block configuration here
        ...
    }
}

There are some others settings options are available. You can add based on you use case or that suites for you website.

  • The gzip_vary directive is used to indicate whether the “Vary” header should be added to the response indicating that the response has been encoded with gzip. The default value is “off” which means that the Vary header is not added. If you want to add the Vary header you should set the gzip_vary directive to “on”

  • The gzip_buffers directive sets the number and size of buffers used for gzip compression. The default value is “4 8k”. The values you provided “16 8k” will allow Nginx to use 16 buffers of 8KB each for compression, which may improve compression performance at the expense of increased memory usage.

  • The gzip_http_version directive is used to specify which version of HTTP to use when sending the “Vary” header. The default value is “1.0” but you can also use “1.1” to indicate that the response is encoded with gzip and the client should use HTTP/1.1 when sending subsequent requests.

  • The gzip_min_length directive is used to specify the minimum length of content that will be compressed. The default value is “20” but you can adjust it to improve performance. A higher value means that only larger files will be compressed, which can decrease CPU usage and improve performance, but it will also decrease the overall compression ratio.

For example, if you set the gzip_min_length to 1000, it means that only files larger than 1000 bytes will be compressed.

Notes:

It’s important to keep in mind that enabling text compression on your server can greatly improve the speed and performance of your website, but it can also increase the CPU usage on your server. It’s recommended to monitor your server’s performance after enabling text compression and adjust the compression level and parameters accordingly.

How to apply changes in nginx

After making changes to your Nginx configuration file, you will need to apply the changes for them to take effect.

The steps to apply changes in Nginx vary depending on your operating system and the version of Nginx you are running. Here are some common ways to apply changes in Nginx:

  1. Reload the Nginx service: This will cause Nginx to reload its configuration files and apply any changes you have made. The command to reload the Nginx service is usually:
sudo systemctl reload nginx  # for systemd based systems
sudo service nginx reload  # for sysVinit based systems
  1. Restart the Nginx service: Restarting the Nginx service will cause Nginx to stop and then start again, applying any changes you have made. The command to restart the Nginx service is usually:
sudo systemctl restart nginx  # for systemd based systems
sudo service nginx restart  # for sysVinit based systems
  1. Test the Nginx configuration: before restarting or reloading the service you can check if the new configuration is correct by using the following command:
sudo nginx -t

It should return success

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

It’s important to note that reloading or restarting the Nginx service will cause a brief interruption of service while the new configuration is loaded. Make sure to test your changes on a development environment before applying it to the production servers.

Additionally, it’s recommended to keep a backup of the previous configuration in case you need to roll back to it.

Conclusion

enabling text compression on your server is an effective way to improve website speed and performance. It’s a simple and straightforward process that can have a significant impact on the user experience and search engine rankings. Make sure to test your changes on a development environment before applying it to the production servers, and monitor your server’s performance after enabling text compression and adjust the compression level and parameters accordingly.

Tags

#websiteperformance #textcompression #Apache #Nginx #htaccess

Explore More Technology Posts

Secure Your Web App with OAuth2 and JWT

Learn how to secure your web application using OAuth2 and JWT for robust authentication and authorization.

Read More
Understanding JWT (JSON Web Token)

Learn how JWT (JSON Web Token) works, its benefits, and real-world use cases for secure, stateless authentication in web and mobile apps.

Read More
Navigating the World of Offline UPI Payments: A Step-by-Step Guide

Learn to make UPI payments offline using USSD codes - a simple, secure way to transact without internet. Ideal for areas with poor connectivity.

Read More
ChatGPT vs. Google Bard: Which Large Language Model is Right for You?

ChatGPT and Google Bard are two powerful large language models that can be used for a variety of purposes. This blog post compares the two models and…

Read More
Download Website Source Codes: A Web Developer's Guide

Learn how to efficiently download website source codes using methods like Wget, browser extensions, and website downloader tools. Empower your web de…

Read More
What is Instagram Threads and how does it work?

Discover Threads, Meta's new microblogging app for sharing updates, photos, and engaging in public conversations. Download now!

Read More