Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of gzip configuration for getting started with Nginx

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "the method of gzip configuration for the introduction of Nginx Foundation". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope that this article "the method of gzip configuration for the introduction of Nginx Foundation" can help you solve the problem.

Preface

Gzip (gnu-zip) is a compression technology. After gzip compression, the page size can be reduced to 30% or less, so that users can browse the page much faster. The compressed page of gzip needs to be supported by both the browser and the server, which is actually compressed on the server side, decompressed and parsed by the browser after it is transmitted to the browser. We don't need to worry about browsers, because most browsers now support parsing gzip pages.

Whether it is the front end or the back end, nginx is often used when deploying projects, and small projects often do reverse proxies and so on. Today, let's be simple and straightforward and talk about one of the points-gzip. If there are any mistakes, please correct them.

Ubuntu, centos, linux and so on are commonly used on the server side. If you don't have a server, you can play with it locally.

Mac installation

Under mac, you can install it directly with brew. If you don't install brew, you can install it first.

/ usr/bin/ruby-e "$(curl-fssl https://raw.githubusercontent.com/homebrew/install/master/install)"brew install nginx

Default configuration

After the installation is complete, you will see that the command line contains the following information, and you can see the path where nginx.conf is located, which is the configuration file we are looking for. It also contains port information. When you start nginx, you can directly access http://localhost:8080,. When you see the page shown in the following figure, the installation starts successfully.

Docroot: / usr/local/var/wwwdefault config: / usr/local/etc/nginx/nginx.confport:8080

Several commonly used commands

Launch: nginx

Hot restart: nginx-s reload

Close: nginx-s stop

Test syntax: nginx-t

Gizp configuration:

Gzip-related configurations can be placed at the http {} or server {} or location {} levels. If different levels are repeated, the priority can be set to location {} > server {} > http {}.

1. Enable gzip compression

Gzip on

2. Gzip http version

Gzip_http_version 1.0

3. Prohibit ie6 from compressing gzip (of course, almost no one uses ie6 now)

Gzip_disable "msie [1-6]"

4. Compression level (1-9), which is generally used to balance file size and cpu. 5 is a common value, which is of course related to the actual machine)

Gzip_comp_level 5

5. Minimum compression threshold (20 bytes by default)

Gzip_min_length 20

6. Compress the client data linked through the agent

Gzip_proxied any

7. Using the http protocol version, the default is 1.1. requests for 1.0 will not be compressed. If set to 1.0, it means that all versions above http1.0 will be compressed. (if proxy_pass is used for reverse proxy, then nginx and the back-end upstream server communicate by default using the http/1.0 protocol.)

Gzip_http_version 1.0

8. Proxy cache compression and original version resources to avoid client errors due to the fact that accept-encoding does not support gzip (gzip is generally used now)

Gzip_vary on

9. Compressed file type (the text/html type is always compressed by default. It is specified that it is best to add both application/javascript and text/javascript. If the type of the script tag of the page is different, some js files may not be compressed. The default type is application/javascript)

Gzip_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/javascript text/x-component

10. If you have compressed (.gz) or provide static file service, you can set it to on. If this is not the case, it is best to set it to off, as this will incur additional iUniver overhead. A better way to handle it is to handle it separately at the location {} or server {} level.

Gzip_static on

11. The space used to store the compressed result data stream is shown below. The memory is applied for 16 times of the original data size in 8k units. The default value is to request the same amount of memory space as the original data to store the gzip compression results.

Gzip_buffers 168k; this is the end of the introduction to "the method of gzip configuration for getting started with Nginx". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report