Nginx configures gzip compression to improve website access speed

I installed Nginx on the server myself and installed it directly using yum install nginx. All the default configurations were used without any optimization. Then I uploaded a static HTML website and did domain name resolution. When I accessed the website, I found that the opening speed was very slow. Just now At first, I thought that the local images were too large, so I used a compression tool to compress all the images, which basically reduced the image size by about 50%, but the website access speed did not increase. Later, nginx gzip related configuration was added, and the website access speed was much faster.

 The following is a brief explanation of nginx gzip related configuration:

gzip can compress the css, js, xml, html and other resources of the website during transmission, which can save network bandwidth. Especially when the server is used by individuals and the export bandwidth is relatively low, the effect will be more obvious, but gzip will consume a certain amount. For CPU resources, pay attention to the settings of some values ​​in the configuration.

# Turn on gzip
gzip on;


# Resources below 1kb are not compressed
gzip_min_length 1k;


# Compression level 1-9, the larger the value, the higher the compression rate, but the more CPU resources are consumed
gzip_comp_level 9;


# Which response type resources need to be compressed? Multiple spaces separate
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;


# Add "Vary: Accept-Encoding" response header
gzip_vary on;


# Configure disabling gzip conditions and support regular expressions. This means that gzip is not enabled in ie6 and below
gzip_disable "MSIE [1-6]\.";

 Let’s give a brief explanation of the above configuration:

The two configurations of gzip_comp_level and gzip_types, in the screenshot above, gzip_comp_level is set to 9, and gzip_types also adds image-related compression types. The main reason is that the server where I put the website does not put other services, only a static one. website, so I don’t really care about CPU usage. I just want to ensure that the website can be accessed normally, so the configuration of nginx is biased towards the configuration of the website.
If there are other services on the server that require CPU resources, it is recommended to set gzip_comp_level to about 5. gzip_types does not need to be configured with image-related types. It is best to use a third-party tool for compression. For nginx gzip image compression issues, refer to the screenshot below:

final result:

After turning on gzip, you can see that Content-Encoding is already gzip. Under normal circumstances, website access will be improved to a certain extent.

Guess you like

Origin blog.csdn.net/wangkaichenjuan/article/details/130679628