How to enable gzip compression in nginx server on Linux or Unix system

Here’s how you can enable gzip compression in Nginx server on a Linux or Unix system:

  1. Open the Nginx configuration file, typically located at /etc/nginx/nginx.conf.
  2. In the http block, add the following lines to enable gzip compression:
http {
...
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
...
}

The gzip_types directive defines the MIME types of content that should be compressed. In the example above, gzip compression is enabled for common text and JavaScript files. You can add or remove MIME types as needed. (https://chipedge.com/)

  1. Save the changes and close the file.
  2. Verify the Nginx configuration for any syntax errors by running the following command:
sudo nginx -t
  1. If the configuration is valid, reload the Nginx service to apply the changes:
sudo systemctl reload nginx

After completing these steps, Nginx will compress content using gzip before sending it to the client, which can reduce the size of the response and improve page load times. To verify that gzip compression is working, you can use a tool such as curl to inspect the headers of an HTTP response from the server.

Leave a Comment