Apache to turn on gzip compression and HTTP cache configuration

Source: Internet
Author: User

Tag: gzip

One, Apache open gzip compression First said Gzip configuration method, gzip compression in Apache using the Mod_deflate module,  the following are the specific configuration methods and steps: Step one:   loadmodule  deflate_module modules/mod_deflate.so  loadmodule headers_module modules/mod_ headers.so    Open http.conf, first the above two lines configured before the # number, so that Apache will enable the two modules, where Mod_deflate is a compression module, is to gzip compress the code to be transferred to the client. The role of the     mod_headers module is to tell the browser that the page uses GZIP compression if it does not turn on Mod_ Headers then the browser will download the gzip-compressed page without displaying step two properly:       Add the following code to the httpd.conf, you can add to any blank place, do not know the friend of Apache, if you worry about adding the wrong place, put to the last line of the http.conf file.  <IfModule mod_deflate.c>SetOutputFilter DEFLATE  #必须的, just like a switch, Tell Apache to compress the contents of the  setenvifnocase request_uri&nbsp to the browser. (?: gif|jpe?g|png) $ no-gzip dont-vary  #设置不对后缀gif, jpg,jpeg,png picture files are compressed setenvifnocase  request_uri . (?: Exe|t?gz|zip|bz2|sit|rar) $ no-gzip dont-vary  #同上, is not set to Exe,tgz,gz ... The files are compressed setenvifnocase request_uri . (?:p df|mov|avi|mp3|mp4|rm) $ no-gzip d ont-varyaddoutputfilterbytype deflate text/*  #设置对文件是文本的内容进行压缩, such as text/html text/css  Text/plain et addoutputfilterbytype deflate application/ms* application/vnd* application/ postscript application/javascript application/x-javascript  #这段代码你只需要了解application/javascript  application/x-javascript this paragraph, which means to compress JavaScript files addoutputfilterbytype deflate  application/x-httpd-php application/x-httpd-fastphp  #这段是告诉apache对php类型的文件进行压缩BrowserMatch  ^ mozilla/4 gzip-only-text/html # netscape 4.x  has some problems, so only the compressed file type is text/html Browsermatch  ^Mozilla/4.0[678] no-gzip # Netscape 4.06-4.08  There are more problems, so do not turn on compression browsermatch  The \bmsie !no-gzip !gzip-only-text/html # ie browser will disguise itself as  netscape&nbsp, but in fact it has no problem header  append Vary User-Agent env=!dont-vary  #确保代理不会发送错误的内容 </IfModule>      This configuration here Apache gzip compression is configured to complete, restart aAfter the Pache, the new configuration will take effect.                        Configure HTTP Cache mod_expires Module Step one:            LoadModule expires_module modules/mod_expires.so              in httpd.conf, find the above code and then remove the previous # from step two:      Add the following code to the httpd.conf, you can add to any blank place, do not know the friend of Apache, if you worry about adding the wrong place, put to the last line of the http.conf file. <IfModule mod_expires.c>ExpiresActive On  #激活http缓存, which is a switch, must have some code expiresdefault  a604800  #默认缓存时间为604800秒, that is, 7 days, a means that the client time is expiresbytype text/css a3600  #对css文件缓存3600秒, That is, 1 hours, a means that the client time is the expiresbytype text/html a3600  #对html文件缓存3600秒, that is, 1 hours, a means the client time is the quasi expiresbytype  application/x-javascript A3600  #对javascript文件缓存3600秒, which is 1 hours, a means that the client time is the expiresbytype  image/gif  "Access plus 2 month"   #对gif图片缓存2个月 toThe client time is quasi  @ itxyz.net ExpiresByType image/jpeg  "Access plus 2 month"   #对jpeg和jpg图片缓存2个月 to client time expiresbytype image/png  "Access plus 2 month"  # Cache the PNG image for 2 months, whichever is the client time expiresbytype image/x-icon  "Access plus 2 month"  # The browser small icon cache for 2 months, the client time is quasi expiresbytype application/x-shockwave-flash a2592000  #对flash文件缓存2592000秒, That is, 1 months, a to the client time header unset pragma  #删除掉http头信息中的Pragma, do not understand Google Pragma, he also controls the browser cache, However, it is used for http1.0 standard fileetag noneheader unset etag  #这段代码和上面一段的作用是不使用http1. ETag properties in the 1 standard < filesmatch  "\. (js|css|ico|pdf|flv|jpg|jpeg|png|gif|mp3|mp4|swf) $ ">  #针对js |css|ico, etc. suffix files are set separately #header set  expires  "Thu, 15 apr 2013 20:00:00 gmt" header unset last-modified   #不使用http头信息中的Last-modified property, last-modified refers to the last modified time of the file header append cache-control  "public"   #设置为可被任何缓存区缓存 </filesmatch></ifmodule>      above configuration is my own server is using the configuration method, here to share with you, the code will not be wrong, Finally, don't forget to restart the Apache server Oh   If there is a better configuration of Apache man, please advise!       If you are using a virtual host, and the virtual host supports. htaccess, you can put the above code in the. htaccess file.


This article is from the "-it commune" blog, please be sure to keep this source http://guangpu.blog.51cto.com/3002132/1567533

Apache to turn on gzip compression and HTTP cache 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.