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

h‮ww//:sptt‬w.lautturi.com
How to enable gzip compression in nginx server on Linux or Unix system

To enable gzip compression in an nginx server on a Linux or Unix system, follow these steps:

  1. Connect to the system via SSH as the root user.

  2. Edit the nginx.conf file by running the following command:

nano /etc/nginx/nginx.conf
  1. Find the http block in the file and add the following lines inside it:
gzip on;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

This will enable gzip compression for the specified file types.

  1. Save the file and exit the editor.

  2. Reload the nginx configuration by running the following command:

nginx -s reload

Gzip compression will now be enabled for the nginx server. You can verify that it is working by checking the response headers of a request to the server. The Content-Encoding header should be set to gzip if gzip compression is being applied.

Note: You can adjust the compression level by changing the value of gzip_comp_level. A higher value will result in better compression, but it will also increase the CPU usage.

Created Time:2017-10-16 14:38:45  Author:lautturi