Nginx enables gzip compression (images, files, css)-PHP source code

Source: Internet
Author: User
Gzip compression can help us save bandwidth. It can help us to compress 10 k files to 3 K. This ratio is very high. Next we can see that Nginx enables gzip compression (image, file, css) configuration. Gzip compression can help us save bandwidth. It can help us to compress 10 k files to 3 K. This ratio is very high. Next we can see that Nginx enables gzip compression (image, file, css) configuration.

Script ec (2); script

1. Run Vim to open the Nginx configuration file.

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

2. Find the following section 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

Row 3: Enable Gzip

Row 3: No compression critical value. compression is required if the compression value is greater than 1 kb.

Row 3: buffer, that is, well, you don't have to explain it. You don't have to change it.

Row 3: If a reverse proxy is used, the end communication is HTTP/4th. If you want to use reverse proxy, you don't need to read this popular article. If you have this sentence, just comment it out, the default value is HTTP/1.1.

Row 5th: Compression level, 1-10. The larger the number, the better the compression time. The longer the time is, let's change your mood.

Row 3: the type of the file to be compressed. You just need to fill in what you need. JavaScript can be written in either of the following ways. Some people complain that the js file is not compressed, in fact, just write one more format.

Row 7th: it is related to cache services such as Squid. If on is used, "Vary: Accept-Encoding" will be added to the Header. I don't need this stuff. Let's take a look at it.

Row 8th: IE6 is not very friendly to Gzip and does not provide it with Gzip.

4.: wq is saved and exited, and Nginx is reloaded.

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

5. Use curl to test whether Gzip is enabled successfully

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.111cn.net/"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:13:09 GMTContent-Type: text/html; charset=UTF-8Connection: keep-aliveX-Powered-By: PHP/5.2.17p1X-Pingback: http://www.slyar.com/blog/xmlrpc.phpContent-Encoding: gzip

Page compressed

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.ye111cn.nethemes/default/statics/css/lib.css"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:21:25 GMTContent-Type: text/cssLast-Modified: Sun, 26 Aug 2012 15:17:07 GMTConnection: keep-aliveExpires: Mon, 27 Aug 2012 06:21:25 GMTCache-Control: max-age=43200Content-Encoding: gzip

The css file is compressed successfully.

curl -I -H "Accept-Encoding: gzip, deflate" http://www.111cn.net /Themes/default/statics/js/jquery.min.js"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:21:38 GMTContent-Type: application/x-javascriptLast-Modified: Thu, 12 Jul 2012 17:42:45 GMTConnection: keep-aliveExpires: Mon, 27 Aug 2012 06:21:38 GMTCache-Control: max-age=43200Content-Encoding: gzip

The js file is compressed successfully.

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 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:22:45 GMTContent-Type: image/pngLast-Modified: Thu, 23 Aug 2012 13:50:53 GMTConnection: keep-aliveExpires: Tue, 25 Sep 2012 18:22:45 GMTCache-Control: max-age=2592000Content-Encoding: gzip

Image compressed

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/wp-multicollinks/wp-multicollinks.css"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:23:27 GMTContent-Type: text/cssContent-Length: 180Last-Modified: Sat, 02 May 2009 08:46:15 GMTConnection: keep-aliveExpires: Mon, 27 Aug 2012 06:23:27 GMTCache-Control: max-age=43200Accept-Ranges: bytes

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

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.