Nginx Handbook (V) Static Resources, gzip Compression

Posted by scliburn on Wed, 29 May 2019 11:45:55 +0200

I. Relevant Grammar

# 1. File reading (whether or not to use the kernel to transfer files)
Syntax: sendfile on|off
 Default value: sendfile off
 Context: http,server,location,if in location

#2,tcp_nopush
 # Function: Enhance the transmission efficiency of network packets when sendfile is opened.
# Explanation: When tcp_nopush on, network packets will not be transmitted one by one, but will accumulate more than one transmission, thereby improving transmission efficiency. In large file transfer scenarios, open is recommended.
Syntax: tcp_nopush on|off;
Default value: tcp_nopush off;
Context: http,server,location;

#3,tcp_nodelay
 # Explanation: Contrary to tcp_nopush, data packets are transmitted to users in real time without waiting. In the scenario of high real-time requirement, it is recommended to open.
Syntax: tcp_nodelay on|off;
Default value: tcp_nodelay on;
Context: http,server,location;

#4, compression
 # Function: Compressed transmission
 Grammar: gzip on|off;
Default value: gzip off;
Context: http,server,location,if in location

# 5. Compression Ratio
 Syntax: gzip_comp_level;
Default value: gzip_comp_level 1;
Context: http,server,location

# 5. Controlling the version of gzip compressed http protocol
 Syntax: gzip_http_version 1.0|1.1;
Default value: gzip_http_version 1.1;
Context: http,server,location


# 6. The function of this module is to find the file with the extension ".gz" in the file system with the same url path after receiving the request. 
# For example, http://xxx/homepage.css nginx will first look for the homepage.css.gz file.
# If it exists, send it directly. If it does not exist, compress the stylesheets/homepage.css file by gzip and send it out.
# This avoids duplicate compression and unnecessary resource consumption. This module is not restricted by gzip_types and will be valid for all requests.
# Therefore, it is not recommended to use it globally, because most of them are dynamic requests, and there will be No. GZ file. It is recommended to use it only in local directories where we confirm that there is. gz. 
Syntax: gzip_static on|off
 Default value: gzip_static off
 Context: http,server,location

Use Case (/etc/nginx/conf.d/default.conf)

server {
    listen       80;
    server_name  localhost;

    #File read
    sendfile on;
    
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /opt/site/playSports;
        index  index.html index.htm;
    }

    #Matching pictures
    location ~ .*\.(jpg|gif|png)$ {
        #gzip on;
        #gzip_http_version 1.1;
        #gzip_comp_level 2;
        #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;
        root /opt/site/sam/images;
    }
    
    #Matching document
    location ~ .*\.(txt|xml)$ {
        gzip on;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        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;
        root /opt/site/sam/doc;
    }

    #Matching begins with / download
    location ~ ^/download {
        #gzip_static on;
        #tcp_nopush on; #Merge multiple files for one transfer
        root /opt/site/sam/file;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
When testing gzip_static, the corresponding files need to be gzip compressed first.
[root@sam file]# gzip a.dmg 
[root@sa file]# ls
a.dmg.gz

Topics: Javascript Nginx xml network