Friday 27 December 2019

NGINX: Compress response payload


Using gzip directive, you can compress the response payload.

Example
gzip on;
gzip_comp_level 4;
gzip_types text/plain;
gzip_types application/json;

gzip: Enable or disable zipping of responses
Enables or disables gzipping of responses.

Syntax:  gzip on | off;
Default: gzip off;
Context: http, server, location, if in location

gzip_comp_level: Set zipping compression level
Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9. Higher value means more compression and require more computing resources.

Syntax:  gzip_comp_level level;
Default: gzip_comp_level 1;
Context: http, server, location

gzip_types: Specify MIME types to compress
Syntax: gzip_types mime-type ...;
Default: gzip_types text/html;
Context: http, server, location

Enables gzipping of responses for the specified MIME types. Responses with the “text/html” type are always compressed.

Unless client send 'Accept-Encoding: gzip' header, nginx do not perform zipping of data.
$curl http://localhost:9090/about > temp.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   240  100   240    0     0  37238      0 --:--:-- --:--:-- --:--:-- 40000
$
$
$curl -H 'Accept-Encoding: gzip, deflate' http://localhost:9090/about > temp_compressed.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    63    0    63    0     0   7232      0 --:--:-- --:--:-- --:--:--  7875
$
$ls -lart temp*txt
-rw-r--r--  1 krishna  admin  240 Oct 17 10:38 temp.txt
-rw-r--r--  1 krishna  admin   63 Oct 17 10:38 temp_compressed.txt


As you see the content size of temp.txt and temp_compressed.txt, temp.txt has file size of 240 bytes, whereas temp_compressed.txt has the file size of 63 bytes.


Previous                                                    Next                                                    Home

No comments:

Post a Comment