Nginx turns on Gzip compression to greatly improve page loading speed

1. Vim opens the Nginx configuration file

vim /usr/local/nginx/conf/nginx.conf

2. Find the following paragraph and modify it

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application /x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";

3. Explain

the first Line 1: Turn on Gzip

Line 2: Do not compress the critical value, only those larger than 1K are compressed, generally do not need to be changed

Line 3: buffer, that is, um, forget it, no need to change it

Line 4: Use a reverse proxy If it is, the terminal communication is HTTP/1.0, and you should not need to read my popular science article if you need it; if you have this sentence, just comment it. The default is HTTP/1.1

Line 5: Compression level, 1-10, the larger the number The better the compression, the longer the time. You can change it according to your mood

. Line make up for whatever is missing. There are two ways to write JavaScript. There is no compression, in fact, it is enough to write one more format

Line 7: It is related to caching services such as Squid. If it is on, "Vary: Accept-Encoding" will be added to the Header. I don't need this stuff. Let's figure

it out. Line 8: IE6 is not very friendly to Gzip. Don't give it Gzip



4. :wq save and exit, reload Nginx

/usr/local/nginx/sbin/nginx -s reload



5. Use curl to test whether Gzip is successfully turned on

curl -I -H "Accept-Encoding: gzip, deflate " "http://www.slyar.com/blog/"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:13:09 GMT
Content-Type: text/html; charset= UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.2.17p1
X-Pingback: http://www.slyar.com/blog/xmlrpc.php
Content-Encoding: gzip

page successfully compressed



curl -I - H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/photonic/include/css/photonic. css"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:25 GMT
Content-Type: text/css
Last-Modified: Sun, 26 Aug 2012 15:17:07 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:25 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip

css文件成功压缩

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-includes/js/jquery/jquery.js"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:38 GMT
Content-Type: application/x-javascript
Last-Modified: Thu, 12 Jul 2012 17:42:45 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:38 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip

js文件成功压缩

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/uploads/2012/08/2012-08-23_203542.png"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:22:45 GMT
Content-Type: image/png
Last-Modified: Thu, 23 Aug 2012 13:50:53 GMT
Connection: keep-alive
Expires: Tue, 25 Sep 2012 18:22:45 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip

图片成功压缩

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/wp-multicollinks/wp-multicollinks.css"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:23:27 GMT
Content-Type: text/css
Content-Length: 180
Last-Modified: Sat, 02 May 2009 08:46:15 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:23:27 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

The last file is less than 1K. Since my threshold is 1K, it is not compressed

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326395250&siteId=291194637