Enable Gzip compression in Nginx on Ubuntu

To enable Enable Gzip compression in Nginx on Ubuntu you will have to modify nginx.config file on your Ubuntu server.

Gzip compression

Gzip compression is a method of reducing the size of files sent from a web server to your computer. It works by compressing the data in the file, making it smaller and faster to transfer. When you request a page from a website, your browser sends a request to that website’s server. The server then sends back an HTTP response with instructions on how to uncompress the file so it can be displayed in your browser. This reduces the amount of data that needs to be transferred, making websites faster and more efficient.

Gzip compression advantages

Utilizing Gzip compression offers numerous advantages for your website, such as:

  • Accelerated page loading: Compressing files reduces their size and facilitates faster transfer, thereby considerably decreasing page loading times.
  • Reduced bandwidth consumption: Compressed files consume less bandwidth, which can minimize server expenses and enhance the user experience for visitors with slower internet connections.
  • Enhanced SEO: Faster page loading times can enhance your website’s search engine rankings, leading to increased traffic and engagement.

Steps to enable Gzip compression

To enable Gzip compression in Nginx on Ubuntu, please follow these steps:

Open the global nginx configuration file for your website. The location for this file on Ubuntu or other Linux distributions is /etc/nginx/nginx.conf. Run the following command:

sudo nano /etc/nginx/nginx.conf

To enable Gzip compression, scroll down the http block and find the Gzip section, it will look like this:

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Modify that code to this:

## Gzip Settings
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Explanation:

  • gzip on; enables Gzip compression.
  • gzip_vary on; adds the “Vary: Accept-Encoding” header to the response, directing caches to store both compressed and uncompressed file versions.
  • gzip_proxied any; allows Gzip compression to be utilized with any proxy server.
  • gzip_comp_level 6; sets the compression level to 6, which balances file size and compression time effectively.
  • gzip_buffers 16 8k; specifies the size of the compression buffers.
  • gzip_http_version 1.1; sets the minimum HTTP version required to compress files.
  • gzip_types; indicates the file types that should be compressed, including text files, JSON, and XML.

Save the file by pressing the following key combination:

  • CTRL + O
  • CTRL + Enter

The editor will give you the status like [ Wrote 83 lines ]. Press CTRL + X to exit out of nano editor.

To make sure there are no configuration errors, test your nginx configuration by running sudo nginx -t command:

sudo nginx -t

If the syntax is ok it will give you the following prompt:

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

Run the sudo systemctl reload nginx command tr reload the nginx:

sudo systemctl reload nginx

Gzip has been enabled now. To verify that, you can verify any resource (supported by Gzip) in dev tools:

Gzip has been enabled
Gzip has been enabled

The response’s content encoding will confirm about the gzip status for that file from the Ubuntu server.

By incorporating the Gzip settings into the global nginx.conf file, all server blocks or virtual hosts will automatically inherit the Gzip settings. Thus, Gzip compression will be enabled for all websites hosted on the server.