就像我们在自己的计算机上压缩文件一样,我们的网络文件也可以被压缩。而且压缩率相对较高,可以节省大量的服务器带宽。
通常在服务器上使用gzip压缩。他可以压缩以下文件类型:
HTML:。html和其他动态html文件,如php.aspx等人
文本文件:。txt等
CSS和贾瓦斯克里普特:extensions.css和。js
Web服务:WSDL、REST和JSON
测试我们的网站是否打开gzip压缩可以使用web工具进行测试:
http://tool.chinaz.com/Gzips/
在此页面上输入您的URL,他可以在线检查,如果他打开压缩。以下是此站的查询结果。
开启gzip压缩的代码
apache服务器
<IfModule mod_deflate.c> # Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml # Remove browser bugs (only needed for really old browsers) BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent </IfModule>
将代码写入.htaccess即可
nginx服务器
gzip on; gzip_comp_level 2; gzip_http_version 1.0; gzip_proxied any; gzip_min_length 1100; gzip_buffers 16 8k; gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_disable "MSIE [1-6].(?!.*SV1)"; gzip_vary on; 将代码写入nginx.conf配置文件即可。
除非注明,否则均为优站网原创文章,转载必须以链接形式标明本文链接
本文链接:https://www.zhanceo.com/15213.html