To enable gzip compression in an nginx server on a Linux or Unix system, follow these steps:
Connect to the system via SSH as the root user.
Edit the nginx.conf
file by running the following command:
nano /etc/nginx/nginx.conf
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.
Save the file and exit the editor.
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.