Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To enable text compression on your Nginx proxy server, you can use the gzip module. Follow these steps to enable and configure gzip compression:

Open the Nginx configuration file (nginx.conf) using a text editor. The configuration file is usually located at /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf.

In the http block of the configuration file, add the following lines to enable gzip compression:

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;

Here's a brief explanation of these directives:

gzip on;: Enables gzip compression. gzipvary on;: Enables the Vary header, which informs proxies that the compressed and uncompressed versions of a file are different. gzipproxied any;: Enables compression for all proxied requests, regardless of the response header. gzipcomplevel 6;: Sets the compression level (1 to 9) with 1 being the least compression and 9 being the most. Level 6 is a good balance between compression ratio and CPU usage. gzipbuffers 16 8k;: Sets the number and size of buffers used for compression. In this case, 16 buffers each of 8KB in size. gziphttpversion 1.1;: Sets the minimum HTTP version of the request required for gzip compression. gziptypes ...;: Lists the MIME types to be compressed. Add or remove MIME types as needed for your specific use case. Save and close the configuration file.

Test the configuration for any syntax errors by running:

sudo nginx -t

If the test is successful, reload the Nginx service for the changes to take effect:

sudo service nginx reload

or

sudo systemctl reload nginx

Now, your Nginx proxy server should compress the specified text content types when serving your site.