如何在Web服务器上节省流量



加载的Web项目消耗了数TB的流量。对于大量用户,节省10%至20%可以节省大量资金,并且有助于避免超出配额。
如果流量危险地达到托管费率的限制甚至超过限制,我该怎么办?

在本文中,我们将分析有助于节省Web服务器流量的基本技术。

挤一下!


节省流量的最简单方法是压缩流量。这样会加载服务器处理器,但允许您更快地将数据发送到客户端,从而减小客户端的大小,从而可以更快地关闭连接。通常使用与Deflate兼容的算法,但也有奇异的算法。

Gzip


最常见的压缩算法。无损压缩,具有良好的压缩率(可配置为1到9,默认为6)和快速解压缩。简单有效,适合大多数情况。



Nginx的

gzip            on;
gzip_min_length 1000;
gzip_proxied    expired no-cache no-store private auth;
gzip_types      text/plain application/xml;

阿帕奇

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

佐普利


gzip的现代替代品压缩效果更好,但压缩速度要快3-8%(在客户端上以相同速度解压缩)。它可以在Deflate上运行,因此与zlib 100%兼容,并且对浏览器的支持也很丰富。

git clone https://code.google.com/p/zopfli/
cd zopfli
make

Nginx的

gzip_static     on;

布罗特利


就像Zopfli一样,是在Google的肠道中开发的。不仅可以静态压缩,还可以动态压缩,例如gzip。与以前的算法不同,它不仅搜索文本中的重复项,而且还立即映射其自己的字典,该字典具有许多标签和标准代码短语,对于html / css / js压缩极为有效:如果Zopfli在gzip后给出大约8%的压缩率,那么Brotli可以再扔10-15%,而其他人则可以扔23%!但是它仅在https中受支持,并且与zlib / deflate不兼容。Caniuse令人鼓舞: 仅在Plus中提供标准模块形式的



Nginx

支持,常规Nginx必须使用第三方模块构建(--add-module = / path / to / ngx_brotli):

git clone https://github.com/google/ngx_brotli.git
git clone https://github.com/bagder/libbrotli.git
./autogen.sh
./configure
make


cd /path/to/nginx
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-stream_geoip_module=dynamic --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed' --add-module=/path/to/ngx_brotli
make

配置:

brotli_static	on;

在动态模式下:

brotli		on;
brotli_comp_level	6;
brotli_types	text/plain text/css text/xml application/x-javascript;

Apache

Everything在这里更简单,安装mod_brotli并配置模块:

<IfModule brotli_module>
 BrotliCompressionLevel 10
 BrotliWindowSize 22
 AddOutputFilterByType BROTLI text/html text/plain text/css text/xml
 AddOutputFilterByType BROTLI text/css
 AddOutputFilterByType BROTLI application/x-javascript application/javascript
 AddOutputFilterByType BROTLI application/rss+xml
 AddOutputFilterByType BROTLI application/xml
 AddOutputFilterByType BROTLI application/json
 </IfModule>

快取吧!


您还可以卸载用户和服务器之间的通道,从而最大程度地减少了重新加载资源的需求。如果文件被缓存,则在下一个请求时,浏览器将在本地接收其内容。

HTTP标头Cache-control,Expires和Vary允许您设计非常灵活的缓存策略,尽管您可以在额头上的任意位置放置max-age = 2592000。

Nginx的

location ~* ^.+\.(js|css)$ {
	expires max;
}

阿帕奇

<ifModule mod_headers.c>
    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "max-age=43200"
    </FilesMatch>
    <FilesMatch "\.(js|css|txt)$">
        Header set Cache-Control "max-age=604800"
    </FilesMatch>
    <FilesMatch "\.(flv|swf|ico|gif|jpg|jpeg|png)$">
        Header set Cache-Control "max-age=2592000"
    </FilesMatch>
    <FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
        Header unset Cache-Control
    </FilesMatch>
</IfModule>
<ifModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 5 seconds"
    ExpiresByType image/x-icon "access plus 2592000 seconds"
    ExpiresByType image/jpeg "access plus 2592000 seconds"
    ExpiresByType image/png "access plus 2592000 seconds"
    ExpiresByType image/gif "access plus 2592000 seconds"
    ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
    ExpiresByType text/css "access plus 604800 seconds"
    ExpiresByType text/javascript "access plus 604800 seconds"
    ExpiresByType application/javascript "access plus 604800 seconds"
    ExpiresByType application/x-javascript "access plus 604800 seconds"
    ExpiresByType text/html "access plus 43200 seconds"
    ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>

分发!


对于繁忙的站点而言,一个好的CDN通常会花费很多钱,但是第一次免费就足够了。通过CDN下载大量资源可以使流量减少数倍!不要忽视这个机会,尤其是在使用共享软件时。Internet上有很多关于免费网络顶部的一次性文章,但是Cloudflare始终是第一位的。



他们也有免费的服务人员,您可以通过这些服务手动提高您的CDN偏好和礼貌。例子很少,但该教程位于官方的SW门户上。

结论


如果您至少没有使用gzip,欢迎访问Internet,这里80%以上的网站都可以使用它。如果您没有足够的标准压缩比和-9,请使用Brotli和Zopfli形式的备份(因为Brotley尚没有100%的覆盖率)。这样可以节省大量流量:

  • gzip:根据内容压缩50-95%网路平均65-80%
  • Zopfli:相对于gzip,平均压缩幅度为3-8%,但发生率为10%
  • Brotli:相对于gzip,压缩量为10-15%,具体取决于内容,罕见镜头的压缩率最高可达20%

图片

在客户端上缓存数据,这将使重复访问期间的流量减少99%或更低,具体取决于所选的缓存策略和站点上的更改。

使用CDN进行内容交付和基本平衡。分发服务器首当其冲,而主要流量将减少数倍。具体多少取决于网络,负载和所选的运行模式。

以上所有操作均在最短的时间内完成,不需要对服务器体系结构进行复杂的重组,从而为您的性能设计提供了时间。压缩,缓存,分发和监控您的费用,这样您就不会赚很多钱。


All Articles