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