Nginx basic introduction to gzip configuration method

PHPz
Release: 2023-06-03 09:52:43
forward
4217 people have browsed it

Preface

gzip (gnu-zip) is a compression technology. After gzip compression, the page size can be reduced to 30% or even smaller than the original size. In this way, users will browse the page much faster. The gzip compressed page needs to be supported by both the browser and the server. It is actually server-side compression. After being transmitted to the browser, the browser decompresses and parses it. We don’t need to worry about the browser, because most current browsers 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 use a reverse proxy or something. Today I will be simple and direct and talk about one of the points - gzip. If there are any errors, please correct me.

Ubuntu, centos, linux, etc. are commonly used on the server side. If you don’t have a server, you can play it locally.

mac installation

You can install it directly with brew under mac. If brew is not installed, you can install it first

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

Default configuration

After the installation is completed, you will see that the command line contains the following information. You can see the path where nginx.conf is located. This is the configuration file we are looking for. It also contains port information. When starting nginx, you can directly access http://localhost:8080. When you see the page shown in the figure below, the installation is started successfully.

docroot: /usr/local/var/www
default config: /usr/local/etc/nginx/nginx.conf
port:8080
Copy after login

Nginx basic introduction to gzip configuration method

Several common commands

  • Start: nginx

  • Restart warmly: nginx -s reload

  • Shut down: nginx -s stop

  • Test syntax: nginx -t

##gizp configuration:

gzip related configuration can be placed at the

http{} or server{} or location{} level, if there are duplicate settings at different levels The priority is location{} > server{} > http{}

1, enable gzip compression


gzip on;
Copy after login

2, gzip http version


gzip_http_version 1.0;
Copy after login
Copy after login

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


gzip_disable "msie [1-6]";
Copy after login

4. Compression level (1~9, generally balanced file size When used with CPU, 5 is a commonly used value, which of course depends on the actual machine)


gzip_comp_level 5;
Copy after login

5. Minimum compression threshold (the default is 20 bytes)


gzip_min_length 20;
Copy after login

6. Compress client data linked through the proxy


gzip_proxied any;
Copy after login

7. The default http protocol version is 1.1, and requests for 1.0 will not be compressed. If set to 1.0, it means http1.0 or above The versions will be compressed. (If proxy_pass is used for reverse proxy, the communication between nginx and the back-end upstream server uses the http/1.0 protocol by default.)


gzip_http_version 1.0;
Copy after login
Copy after login

8. Proxy cache compression and original version resources, Avoid client errors due to accept-encoding not supporting gzip (gzip is generally used now)


gzip_vary on;
Copy after login

9. Compressed file type (the default is always to compress text/html type, which is specially stated It is best to add both application/javascript and text/javascript. If the types of script tags on the page are 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;
Copy after login

10. If there are compressed (.gz) or static file services, you can set it to on. If this is not the case, it is best to set it to off as this will cause additional I/O overhead. A better way to handle it is to handle it separately at the

location{} or server{} level.

gzip_static on;
Copy after login

11. The space used to store the compression result data stream is expressed as 8k as the unit. Apply for memory in 16 times the original data size in 8k units. The default value is to apply for the same size memory space as the original data to store the gzip compression result.


gzip_buffers 16 8k;
Copy after login

The above is the detailed content of Nginx basic introduction to gzip configuration method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template