Nginx optimizes static file access

Keywords: xml JSON Javascript Nginx

brief introduction

The static files needed in web development are: CSS, JS, font and picture, which can be accessed through web framework, but the efficiency is not optimal.
Nginx is much more efficient than Web framework in processing static files, because it can use gzip compression protocol to reduce the volume of static files, speed up the loading speed of static files, open cache and timeout time, and reduce the number of requests for static files.

The following describes how to manage static file access through Nginx and optimize website access speed.

1, Open gzip

The configuration and parameters are as follows. It is recommended to delete the comments when using.

gzip on;
#This command is used to turn the gzip module on or off

gzip_buffers 16 8k;
#Set the system to get several units of cache to store the compressed result data stream of gzip. 16 8k represents 16 times of the requested memory in 8k and the original data size of the installation is 8k

gzip_comp_level 6;
#gzip compression ratio, the numerical range is 1-9, 1 compression ratio is the smallest but the processing speed is the fastest, 9 compression ratio is the largest but the processing speed is the slowest

gzip_http_version 1.1;
#Identify the protocol version of http

gzip_min_length 256;
#Set the minimum number of bytes allowed to be compressed on the page. The number of bytes on the page is obtained from the content length of the header. The default value is 0, no matter how many pages are compressed. Here I set it to 256

gzip_proxied any;
#It is set here to enable compression unconditionally regardless of the header header

gzip_vary on;
#Add variety: accept encoding to http header for proxy server

gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
#The type of file to be compressed. In particular, the file type of font is added here

gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#Disable IE 6 gzip


2, Extended compression type

Modify the / etc/nginx/mime.types file, and add the corresponding type of the file to be compressed to the above gzip configuration. Almost all types of static files are covered below:

types {
    application/atom+xml                atom;
    application/dart                    dart;
    application/gzip                    gz;
    application/java-archive            jar war ear;
    application/javascript              js jsonp;
    application/json                    json;
    application/owl+xml                 owl owx;
    application/pdf                     pdf;
    application/postscript              ai eps ps;
    application/rdf+xml                 rdf;
    application/rss+xml                 rss;
    application/vnd.ms-fontobject       eot;
    application/x-7z-compressed         7z;
    application/x-bittorrent            torrent;
    application/x-chrome-extension      crx;
    application/x-font-otf              otf;
    application/x-font-ttf              ttc ttf;
    application/x-font-woff             woff;
    application/x-opera-extension       oex;
    application/x-rar-compressed        rar;
    application/x-shockwave-flash       swf;
    application/x-web-app-manifest+json webapp;
    application/x-x509-ca-cert          crt der pem;
    application/x-xpinstall             xpi;
    application/xhtml+xml               xhtml;
    application/xml                     xml;
    application/xml-dtd                 dtd;
    application/zip                     zip;

    audio/midi                          kar mid midi;
    audio/mp4                           aac f4a f4b m4a;
    audio/mpeg                          mp3;
    audio/ogg                           oga ogg;
    audio/vnd.wave                      wav;
    audio/x-flac                        flac;
    audio/x-realaudio                   ra;

    image/bmp                           bmp;
    image/gif                           gif;
    image/jpeg                          jpe jpeg jpg;
    image/png                           png;
    image/svg+xml                       svg svgz;
    image/tiff                          tif tiff;
    image/webp                          webp;
    image/x-icon                        cur ico;

    text/cache-manifest                 appcache manifest;
    text/css                            css less;
    text/csv                            csv;
    text/html                           htm html shtml;
    text/mathml                         mml;
    text/plain                          txt;
    text/rtf                            rtf;
    text/vcard                          vcf;
    text/vtt                            vtt;
    text/x-component                    htc;
    text/x-markdown                     md;

    video/3gpp                          3gp 3gpp;
    video/avi                           avi;
    video/mp4                           f4p f4v m4v mp4;
    video/mpeg                          mpeg mpg;
    video/ogg                           ogv;
    video/quicktime                     mov;
    video/webm                          webm;
    video/x-flv                         flv;
    video/x-matroska                    mkv;
    video/x-ms-wmv                      wmv;
}

3, Opening timeout

Turn on caching by setting Expires.

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
}

location ~ .*\.(eot|ttf|otf|woff|svg)$ {
    expires 30d;
    access_log off;
}

location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
}

Reprint address: https://www.cnblogs.com/xueweihan/p/7082832.html

Posted by cent on Mon, 04 May 2020 04:54:50 -0700