Excellent software and practical tutorials
Enable Gzip compression for your website to effectively save 30% traffic and increase access speed
Gzip It is a technology for website compression and acceleration. After it is turned on, it can speed up the opening speed of our website. The principle is that the server compresses and the client browser quickly decompresses it, which can greatly reduce the traffic of the website.
Nginx enables the Gzip compression function, which can compress the website's css, js, xml, and html files during transmission, improve access speed, and optimize Nginx performance! Because the compression effect is not good for pictures, videos and other multimedia files and large files on the Web site, there is no need to support compression for pictures. If you want to optimize, you can set the life cycle of the picture to be longer and let the client cache it. After the Gzip function is enabled, the Nginx server will compress the sent content, such as static resources such as css, js, xml, and html, according to the configured strategy, so that the size of these contents is reduced, and the returned content is processed before the user receives it, and the compressed data is presented to the customer. This can not only save a lot of export bandwidth and improve transmission efficiency, but also improve the user's fast perception experience, killing two birds with one stone; although it will consume a certain amount of CPU resources, it is still worth it to give users a better experience.
Gzip Configuration Parameters
go throughGzip CompressionAfter that, the page size can be reduced to the original size or even smaller, so that users can browse the page much faster. Gzip compressed pages need to be supported by both the browser and the server. In fact, it is compressed on the server side, and the browser decompresses and parses it after it is transmitted to the browser. We don't need to worry about the browser, because most browsers currently support parsing Gzipped pages.
Gzip compression function: before sending the response message to the client, you can enable the compression function, which can effectively save bandwidth and increase the speed of responding to the client. Gzip compression can be configured in http, server and location modules. Nginx Gzip compression configuration parameter description:
gzip on; #Whether to enable the gzip module on means on and off means offgzip_buffers 4 16k; #Set the buffer size required for compressiongzip_comp_level 6; #Compression level 1-9, the larger the number, the better the compression, and the more CPU time it takesgzip_min_length 1k; #Set the minimum bytes allowed for compressiongzip_http_version 1.1; #Set the version of the compressed http protocol, the default is 1.1gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; #Set the compressed file typegzip_vary on; #Add the http header information Vary: Accept-Encoding to let the backend proxy server identify whether gzip is enabled gzip_disable "MSIE [1-6]\."; #Configures gzip disabled, supports regular expressions. This means that gzip is not enabled for ie6 and below (because lower versions of ie do not support it) gzip_proxied off; #Enabled when nginx is used as a reverse proxy, off (disables compression of all proxy result data), expired (enables compression if the header includes the "Expires" header information), no-cache (enables compression if the header includes "Cache-Control: no-cache"),
no-store (compression is enabled, the header contains "Cache-Control: no-store"), private (compression is enabled, the header contains "Cache-Control: private"), no_last_modefied (compression is enabled, the header does not contain
"Last-Modified"), no_etag (enable compression if the header does not contain the "Etag" header information), auth (enable compression if the header contains the "Authorization" header information)
Edit nginx configuration file
[root@uzbox ~]# vim /usr/local/nginx/conf.d/www.conf http { gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 9; gzip_vary on; gzip_disable "MSIE [1-6]\."; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; }
Reload configuration file
systemctl reload nginx
Check if Gzip is enabled
[root@uzbox ~]# curl -I -H"Accept-Encoding: gzip, deflate" "https://uzbox.com" HTTP/2 200 server: nginx/1.18.0 date: Wed, 30 Nov 2022 05:19:05 GMT content-type: text/html; charset=UTF-8 content-length: 40429 x-powered-by: PHP/7.4.19 vary: Accept-Encoding, Cookie cache-control: max-age=3, must-revalidate content-encoding: gzip last-modified: Wed, 30 Nov 2022 04:27:54 GMT strict-transport-security: max-age=31536000
As shown above, "Conten_Encoding: gzip" appears in the response header, which means that Nginx has enabled compression (the same is true when accessing the request in the browser and viewing the response header through F12)
After Nginx turns on the Gzip compression function, the size of the defined gzip type file during transmission becomes significantly smaller, which will greatly improve the nginx access performance.
Web page GZIP compression detection
You can check Gzip compression status through the following website.