Nginx optimized static file access

Source: Internet
Author: User
Tags json rar webp zip
IntroductionWeb development needs static files are: CSS, JS, fonts, pictures, can be accessed through the web framework, but the efficiency is not optimal.
Nginx is much more efficient at working with static files than the WEB framework, because you can use the GZIP compression protocol to reduce the volume of static files to speed up static file loading, turn on caching, and time out to reduce the number of requests for static files.

The following describes how to manage static file access through Nginx to optimize the site access speed.

First, open the Gzip configuration introduction and parameters as follows, recommended to delete the comments when using.

gzip on;
#该指令用于开启或关闭gzip模块 (on/off)

gzip_buffers 8k;
#设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流. 8k for 8k, install the original data size in 8k units of 16 times times the application of memory

Gzip_comp_level 6;
#gzip压缩比, the numerical range is the 1-9,1 compression ratio of the smallest but the fastest processing speed, 9 compression ratio of the largest but the slowest processing speed

Gzip_http_version 1.1;
#识别http的协议版本

gzip_min_length;
#设置允许压缩的页面最小字节数, the number of page bytes is obtained from the header content-length. The default value is 0, regardless of the number of pages in most compression. Here I set up for the

gzip_proxied any;
#这里设置无论header头是怎么样, all are unconditionally enabled compression

gzip_vary on;
Add vary:accept-encoding to #在http header,

gzip_types
    text/xml application/xml application/atom+xml for proxy server 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;
#进行压缩的文件类型, here special added to the font file type

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


second, the extended compression type modifies the/etc/nginx/mime.types file, adding the file that needs to be compressed corresponds to the type in the above gzip configuration. The following covers almost all of the static file corresponding types:

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; }

Third, open time-outOpen the cache 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.