Nginx optimized--gzip compression and expire browser cache

Source: Internet
Author: User

Gzip Compression Overview

Web pages in the server side after gzip or other format of the compressed output significantly reduced content-length bytes, when the access to millions, these reduced bytes will become objective traffic to save, thereby reducing the pressure on the server and the speed of the Web page access;

Principle

When the client sends an HTTP request to the server, it has a accept-encoding header in the request header, which tells it what kind of compressed file it can receive from the customer service side, and if the server side configures the compression requirement, it will return the corresponding compressed file. The browser then decodes the rendering, we do the acquisition, we need to collect uncompressed files, so the HTTP request header does not include the Accept-encoding key;

Nginx compression

Add the following configuration to the HTTP segment

Gzip On|off;#whether to turn on gzipGzip_buffers 4k| 8K#Buffering (How many blocks are compressed in memory?)Gzip_comp_level [1-9]#Recommended 6 compression level (the higher the level, the smaller the pressure, the more wasted CPU computing resources)Gzip_disable#regular match ua What URI does not perform gzipGzip_min_length 200#the minimum length at which to start compressing (no more small, not compressed, meaning not)Gzip_http_version 1.0|1.1#Start Compressed HTTP protocol version (can not be set, currently almost all 1.1 protocol)Gzip_proxied#set the requestor proxy server, how to cache the contentGzip_types Text/plain Application/xml#for which types of files are compressed such as txt,xml,html, CSSGzip_vary On|off#whether to transmit the GZIP compression flag
Example
gzip on;gzip_min_length  1k;gzip_buffers     41.12; gzip_types     text/ Plain Application/javascript application/x-javascript text/javascript text/css application/xml application/xml+  Rss;gzip_vary on;gzip_proxied   expired noprivate  auth;gzip_disable   "MSIE [1-6] \.";

Attention:

    1. Picture/mp3 Such binary files, do not need to be compressed, because the compression ratio is smaller, such as 100->80 bytes, and compression is CPU-intensive resources.
    2. Relatively small files do not have to be compressed, meaning does not exist.
Expire Browser Cache Settings overview

Here the cache control is mainly for the picture, Css,js and other changes in the short period of the static files , for example, when we first visited this image, the server returned 200, while the response header returned two keys, Etag: The file's ' fingerprint ' (unique identifier) and last-modified: ' File modification time '; At this point the browser, as well as the other cache server will put this picture to cache; When requesting this image again, the request header adds two key values, If-modified-since: The last time the change occurred; F-none-match: The ETag value of the last file itself, the server determines its etag and last-modified based on these two key values, and does not return this image if it has not changed, only returns a 304 status code, The server received this 304 status code will go to the cache itself to find the cached image;
This reduces the bandwidth pressure of the server and improves the website access speed;

Configuration

The location segment and if segment can be set

Location ~. *\. (gif|jpg|jpeg|png|bmp| swf) ${    expires      ~. *\. ( JS|CSS)? ${    expires      12h;}

Format

Expires 30s;expires 30m;expires 2h;expires 30d;

Note: The server's date is accurate, and if the server's date falls behind the actual date, the cache may be invalidated

Original address: http://www.cnblogs.com/nixi8/p/4904519.html

Nginx optimized--gzip compression and expire browser cache

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.