“This is the 15th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

Nginx Static Resource Deployment [2]

Previous articles:

I met Nginx

The installation of the nginx

Nginx core configuration file structure

Nginx Static Resource Deployment [1]

Nginx static resource compression

Which is more efficient if we send a Megabyte of data or a Megabyte of data? “The obvious answer is that when you transmit less, you get faster. If the size of the same content is reduced, the word that comes to mind is “compression”. Next, let’s study the static resource compression module of Nginx.

Gzip can be configured to compress static resources in the Nginx configuration file. The related instructions can be configured in the HTTP block, server block, and location block. Nginx can pass

Ngx_http_gzip_module ngX_HTTP_gzip_STATIC_module NgX_HTTP_gunzip_module moduleCopy the code

Gzip module configuration instructions

The following instructions come from the ngx_HTTP_gzip_module module, which is built into the nginx installation environment when nginx is installed, meaning that you can use them directly.

1. Gzip command: This command is used to enable or disable the Gzip function

grammar gzip on|off;
The default value gzip off;
location HTTP, server, location…

Note that the following command has effect only if it is in the open state

http{
   gzip on;
}
Copy the code

2. Gzip_types directive: This directive optionally enables Gzip compression based on the MIME type of the response page

grammar gzip_types mime-type … ;
The default value gzip_types text/html;
location HTTP, Server, location

The selected value can be looked up from the MIme.types file, or “*” can be used to represent all.

http{
    gzip_types application/javascript;
}
Copy the code

3. Gzip_comp_level directive: This directive is used to set the level of Gzip compression. The level ranges from 1 to 9, where 1 indicates the lowest level and the highest efficiency, and 9 is the opposite, which indicates the highest level of compression but the lowest efficiency and time consuming.

grammar gzip_comp_level level;
The default value gzip_comp_level 1;
location HTTP, Server, location
http{
    gzip_comp_level 6;
}
Copy the code

4. Gzip_vary directive: This directive is used to set whether a compressed Gzip send carries the Vary: Accept-encoding header for the response header. Basically, it tells the receiver that the data sent has been compressed by Gzip

grammar gzip_vary on|off;
The default value gzip_vary off;
location HTTP, Server, location

5. The gzip_buffers directive: This directive deals with the number and size of buffers required for compression.

grammar gzip_buffers number size;
The default value gzip_buffers 32 4k
location HTTP, Server, location

Number: specifies the number of cache Spaces requested by the Nginx server. Size refers to the size of each cache space. The main implementation is to apply for a number of each size of memory space. The value varies depending on the operating system of the server. Therefore, you are advised to use the default value.

gzip_buffers 4 16K; # Cache space sizeCopy the code

6. Gzip_disable command: The Gzip function can be selectively enabled or disabled for requests initiated by different types of clients.

grammar gzip_disable regex … ;
The default value
location HTTP, Server, location

Regex: Set based on the user-agent flag of the client. Regular expressions are supported. The specified browser flag does not use Gzip. This directive is generally used to exclude some browsers that clearly do not support Gzip.

gzip_disable "MSIE [1-6].";
Copy the code

7. Gzip_http_version directive: You can optionally enable or disable Gzip for different HTTP versions.

grammar Gzip_http_version | 1.0 1.1;
The default value Gzip_http_version 1.1;
location HTTP, Server, location

This directive is the lowest version of HTTP that specifies the use of Gzip, and the default is generally acceptable.

8. Gzip_min_length directive: This directive selectively turns Gzip on and off for the size of the transmitted data

grammar gzip_min_length length;
The default value gzip_min_length 20;
location HTTP, Server, location
Nignx metering unit: the size of bytes byte [] / [million] KB (kilobytes)/M for example: 1024/10/10 M | | K K MCopy the code

The Gzip compression function has an obvious effect on big data compression. However, if the data to be compressed is relatively small, the more compressed the data, the larger the data volume may be. Therefore, we need to determine whether to use the Gzip function based on the size of the response Content. However, this instruction will be ignored if Chunk encoding is used for dynamic compression. You are advised to set the value to 1K or higher.

9. Gzip_proxied directive: This directive sets whether to Gzip the results returned by the server.

grammar gzip_proxied off|expired|no-cache| no-store|private|no_last_modified|no_etag|auth|any;
The default value gzip_proxied off;
location HTTP, Server, location

Expired – Enable compression if the header contains the “Expires” header no-cache – enable compression If the header contains the “cache-control :no-cache” header information, no-store – compression enabled, if the header contains the” cache-control :no-store” header information, private – compression enabled, If the header header contains the “cache-Control :private” header information no_last_Modified – Enables compression. If the header header does not contain the” last-modified “header information no_etag – Enables compression If the header does not contain the “ETag” header information, auth – Enables compression. If the header contains the “Authorization” header information, any – Enables compression unconditionally

Example configuration of the Gzip compression function

gzip on; Gzip_types *; Set gzip_comp_level 6 based on the type of resource to be accessed. #gzip compression level gzip_min_length 1024; Content-length gzip_buffers 4 16K; # Cache size gzip_http_version 1.1; # specify the lowest version of the HTTP request required to compress the response gzip_vary on; Gzip_disable "MSIE [1-6]."; # do not compress Internet Explorer versions up to 6; #nginx acts as a reverse proxy to compress the data returned by the serverCopy the code

These configurations can be used in many places, so we can extract them into a configuration file, and then load the configuration file again into the nginx.conf configuration file using the include directive.

nginx_gzip.conf

gzip on;
gzip_types *;
gzip_comp_level 6;
gzip_min_length 1024;
gzip_buffers 4 16K;
gzip_http_version 1.1;
gzip_vary  on;
gzip_disable "MSIE [1-6].";
gzip_proxied  off;
Copy the code

nginx.conf

include nginx_gzip.conf
Copy the code

Gzip and SendFile co-exist

Send file is used to open efficient file transfer mode. After sendFile is started, static resource files on disk can be read and the number of copies can be reduced. Static files can be sent out through network devices without user process, but Gzip to compress resources, is to be operated by user process. So how to solve the coexistence problem of two Settings.

This can be resolved using the gzip_static directive of the ngx_HTTP_gzip_STATIC_module module.

Gzip_static instruction

Gzip_static: If a. Gz file with the same name as the resource to be accessed is checked, the content of the. Gz file is returned as a gzip-related header in response.

grammar gzip_static on | off | always;
The default value gzip_static off;
location HTTP, Server, location

Unknown directive “gzip_static” is invalid because Nginx does not add ngx_http_gzip_STATIC_module by default. How to add?

Add modules to Nginx implementation steps

(1) Query the current Nginx configuration parameters

nginx -V
Copy the code

(2) Rename the nginx binaries in the sbin directory of the nginx installation directory

cd /usr/local/nginx/sbin
mv nginx nginxold
Copy the code

(3) Go to the Nginx installation directory

CD/root/nginx/core/nginx - 1.16.1Copy the code

(4) Run make clean to clear the previously compiled content

make clean
Copy the code

(5) Use configure to configure parameters

./configure --with-http_gzip_static_module
Copy the code

(6) Compile using the make command

make
Copy the code

(7) Move the nginx binary execution files in objs directory to the sbin directory in nginx installation directory

mv objs/nginx /usr/local/nginx/sbin
Copy the code

(8) Run the update command

make upgrade
Copy the code

The gzip_static test is used

(1) Access http:// your IP/jquery.js directly

(2) Use the gzip command for compression

cd /usr/local/nginx/html
gzip jquery.js
Copy the code

(3) Visit http:// your IP/jquery.js again