cmc/cleberg.net

My personal web garden & blog.

clone: git clone https://gitbay.org/cmc/cleberg.net.git

main: content/blog/2022-12-01-nginx-compression.org · raw

 1#+date:        [2022-12-01 Thu 00:00:00]
 2#+title:       Nginx Gzip: Compress Text, Nothing Else
 3#+description: Enabling and configuring GZIP compression in Nginx.
 4#+slug:        nginx-compression
 5#+filetags:    :linux:web:
 6
 7* Text Compression
 8
 9Text compression allows a web server to serve text-based resources faster than
10uncompressed data. This can speed up things like First Contentful Paint, Tie to
11Interactive, and Speed Index.
12
13* Enable Nginx Compression with gzip
14
15In order to enable text compression on Nginx, we need to enable it within the
16configuration file:
17
18#+begin_src sh
19nano /etc/nginx/nginx.conf
20#+end_src
21
22Within the =http= block, find the section that shows something like the block
23below. This is the default gzip configuration I found in my =nginx.conf= file on
24Alpine Linux 3.17. Yours may look slightly different, just make sure that you're
25not creating any duplicate gzip options.
26
27#+begin_src conf
28# Enable gzipping of responses.
29#gzip on;
30
31# Set the Vary HTTP header as defined in the RFC 2616. Default is 'off'.
32gzip_vary on;
33#+end_src
34
35Remove the default gzip lines and replace them with the following:
36
37#+begin_src conf
38# Enable gzipping of responses.
39gzip on;
40gzip_vary on;
41gzip_min_length 10240;
42gzip_proxied expired no-cache no-store private auth;
43gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
44gzip_disable "MSIE [1-6]";
45#+end_src
46
47* Explanations of ngx_{httpgzipmodule} Options
48
49Each of the lines above enables a different aspect of the gzip response for
50Nginx. Here are the full explanations:
51
52- =gzip=: Enables or disables gzipping of responses.
53- =gzip_vary=: Enables or disables inserting the "Vary: Accept-Encoding"
54  response header field if the directives gzip, gzip_{static}, or gunzip are
55  active.
56- =gzip_min_length=: Sets the minimum length of a response that will be
57  gzipped. The length is determined only from the "Content-Length" response
58  header field.
59- =gzip_proxied=: Enables or disables gzipping of responses for proxied
60  requests depending on the request and response. The fact that the request is
61  proxied is determined by the presence of the "Via" request header field.
62- =gzip_types=: Enables gzipping of responses for the specified MIME types in
63  addition to "text/html". The special value "*" matches any MIME type (0.8.29).
64  Responses with the "text/html" type are always compressed.
65- =gzip_disable=: Disables gzipping of responses for requests with
66  "User-Agent" header fields matching any of the specified regular expressions.
67  - The special mask "msie6" (0.7.12) corresponds to the regular expression
68    "MSIE [4-6].", but works faster. Starting from version 0.8.11, "MSIE 6.0;
69    ... SV1" is excluded from this mask.
70
71More information on these directives and their options can be found on the
72[[https://nginx.org/en/docs/http/ngx_http_gzip_module.html][Module ngx_{httpgzipmodule}]] page in Nginx's documentation.