After the server is configured, you also need to enable gzip to compress the returned data to speed up the loading of the site.
Here’s how to enable gzip on Nginx (add it to the configuration file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# enable gzip gzip on; # The smallest file with gzip compression enabled, files smaller than the set value will not be compressed gzip_min_length 1k; # gzip compression level, 1-10, the larger the number, the better the compression, and the more CPU time. gzip_comp_level 2; # The type of file being compressed. gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml font/ttf font/otf; # Whether to add Vary: Accept-Encoding to the http header, it is recommended to enable gzip_vary on; # Disable IE 6 gzip gzip_disable "MSIE [1-6]\."; |
More detailed configuration reference: http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html
how to set the gzip compression level?This will be more confusing.Someone did the test:
text/html – phpinfo():
1 2 3 4 5 6 7 8 9 10 |
0 55.38 KiB (100.00% of original size) 1 11.22 KiB ( 20.26% of original size) 2 10.89 KiB ( 19.66% of original size) 3 10.60 KiB ( 19.14% of original size) 4 10.17 KiB ( 18.36% of original size) 5 9.79 KiB ( 17.68% of original size) 6 9.62 KiB ( 17.37% of original size) 7 9.50 KiB ( 17.15% of original size) 8 9.45 KiB ( 17.06% of original size) 9 9.44 KiB ( 17.05% of original size) |
application/x-javascript – jQuery 1.8.3 (Uncompressed):
1 2 3 4 5 6 7 8 9 10 |
0 261.46 KiB (100.00% of original size) 1 95.01 KiB ( 36.34% of original size) 2 90.60 KiB ( 34.65% of original size) 3 87.16 KiB ( 33.36% of original size) 4 81.89 KiB ( 31.32% of original size) 5 79.33 KiB ( 30.34% of original size) 6 78.04 KiB ( 29.85% of original size) 7 77.85 KiB ( 29.78% of original size) 8 77.74 KiB ( 29.73% of original size) 9 77.75 KiB ( 29.74% of original size) |
It can be seen that when the compression level is greater than 1, the effect slowly declines. It is recommended to use compression level 2.
Some tools that can be used for testing:
https://developers.google.com/speed/pagespeed/insights/
https://varvy.com/tools/gzip/
This article was first published by V on 2018-10-06 and can be reprinted with permission, but please be sure to indicate the original link address of the article :http://www.nginxer.com/records/how-to-enable-nginx-gzip-compression/