Nginx Gzip Compression Configuration

Source: Internet
Author: User

Nginx Gzip Compression Configuration

With the development of Nginx, more and more websites use Nginx, so nginx optimization becomes more and more important, today we look at Nginx gzip compression in the end is how to compress it?

Gzip (Gnu-zip) is a compression technique. After gzip compression, the page size can be changed to 30% or smaller, so users can browse the page at a much faster speed. Gzip Compression page requires both browser and server support, in fact, server-side compression, uploaded to the browser after the browser decompression and parsing. There is no need for us to worry about browsers, as the vast majority of browsers currently support the parsing of gzip-over pages.
The compressed output of Nginx is implemented by a set of gzip compression instructions. The relevant instruction is located between the http{and the two curly braces.

gzip on;
This instruction is used to turn the Gzip module on or off (on/off)

Gzip_min_length 1k;
Sets the minimum number of bytes of pages allowed to compress, and 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. It is recommended to set the number of bytes greater than 1k, which may be more or less larger than 1k.

Gzip_buffers 4 16k;
Set up the system to get the compressed result data stream for several units of cache used to store gzip. 4 16k represents a 16k unit, with the original data size of 4 times times 16k of memory.

Gzip_http_version 1.1;
Protocol version to identify HTTP (1.0/1.1)

Gzip_comp_level 2;
gzip compression ratio, 1 compression than the minimum processing speed, 9 compression than the maximum but the slowest processing speed (transmission is faster but the CPU is more expensive)

Gzip_types text/plain application/x-javascript text/css application/xml
Matching MIME types is compressed, and the "text/html" type is always compressed, regardless of whether it is specified.
Gzip_vary on;
and HTTP header has a relationship, add a vary header, to the proxy server, some browsers support compression, and some do not support, so avoid wasting does not support also compression, so according to the client's HTTP header to determine whether the need to compress

The Nginx Configuration gzip section is as follows:
gzip on;
Gzip_min_length 1k;
Gzip_buffers 64k;
Gzip_http_version 1.1;
Gzip_comp_level 6;
Gzip_types text/plain application/x-javascript text/css application/xml;
Gzip_vary on;

The following is the full Nginx configuration:

Events {
Worker_connections 1024;
}


HTTP {
Include Mime.types;
Default_type Application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
server {
Listen 80;
server_name localhost;
Location/{
root HTML;
Index index.html index.htm;
#proxy_pass http://localhost:8080/;
}
Location/api {
Proxy_pass http://localhost:8080;
}
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
}

gzip on;
Gzip_min_length 1k;
Gzip_buffers 64k;
Gzip_http_version 1.1;
Gzip_comp_level 6;
Gzip_types text/plain application/x-javascript text/css application/xml;
Gzip_vary on;

}

The following is a detailed description of 2:

Gzip
Decide whether to open the Gzip module
Param:on|off
Example:gzip on;

Gzip_buffers
Sets the size of the gzip request memory to request memory space in multiples of block size
Param1:int
Param2:int (k) Back unit is K
Example:gzip_buffers 4 8k;

Gzip_comp_level
Set the GZIP compression level, the lower the compression rate of the lower the file compression ratio, the slower the speed of the file compression ratio greater
Param:1-9
Example:gzip_com_level 1;

Gzip_min_length
When the return content is greater than this value, it is compressed using gzip, in K, with 0 o'clock, all pages are compressed
Param:int
Example:gzip_min_length 1000;

Gzip_http_version
Used to identify the version of the HTTP protocol, the earlier browser does not support gzip compression, users will see garbled, so in order to support the previous version added this option, currently this item can be ignored
param:1.0|1.1
Example:gzip_http_version 1.0

Gzip_proxied
Nginx as a reverse proxy when enabled,
Param:off|expired|no-cache|no-sotre|private|no_last_modified|no_etag|auth|any]
Expample:gzip_proxied No-cache;
off– Close all proxy results data compression
expired– enable compression If the header contains "Expires" headers
no-cache– enable compression If the header contains "Cache-control:no-cache" headers
no-store– enable compression If the header contains "Cache-control:no-store" headers
private– enable compression If the header contains "cache-control:private" headers
no_last_modified– enable compression If the header contains "last_modified" headers
no_etag– enable compression If the header contains "ETag" header information
auth– enable compression If the header contains "Authorization" headers
any– unconditionally compress all result data

Gzip_types
Set the MIME type that needs to be compressed, non-set value does not compress
Param:text/html|application/x-javascript|text/css|application/xml
Example:gzip_types text/html;

Nginx Gzip Compression Configuration

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.