Home  >  Article  >  Operation and Maintenance  >  Nginx basic introduction to gzip configuration method

Nginx basic introduction to gzip configuration method

PHPz
PHPzforward
2023-06-03 09:52:434225browse

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

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

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;

2, gzip http version


gzip_http_version 1.0;

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


gzip_disable "msie [1-6]";

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;

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


gzip_min_length 20;

6. Compress client data linked through the proxy


gzip_proxied any;

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;

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;

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;

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;

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;

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete