Apache服务器怎么开启文件压缩

作者: xusx 分类: Web 发布时间: 2022-04-21 09:45 浏览:265

vue项目打包 chunk-vendors.xxx.js 文件过大,如图,400多K:

因为服务器端没有开启文件gzip压缩,导致通过浏览器访问页面时,速度过慢,访客等待时间明显过长(4、5秒)。

为了提高访问体验,必须减少文件加载时间,由于web服务器用的是Apache2,于是,在网站根目录的 .htaccess 文件里增加一行“AddOutputFilterByType ”:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On

  AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/javascript
  
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  
  RewriteRule ^(.*)$ index.php?p=$1 [QSA,PT,L]

</IfModule>

之后强制刷新一下浏览器缓存,重新加载页面,页面完整加载时间减少了约 2 秒,明显地降低了访客的等待时间。同时,也在一定程度上节省了服务端和客户端双方的带宽流量。

chunk-vendors.xxx.js 文件响应头如下:

Accept-Ranges: bytes
Content-Encoding: gzip
Content-Type: application/javascript
Date: Thu, 21 Apr 2022 01:17:37 GMT
ETag: "6f097-5dd15205b1a31-gzip"
Last-Modified: Wed, 20 Apr 2022 12:28:10 GMT
Server: Apache
Vary: Accept-Encoding

相关文章:Nginx服务器怎么开启文件压缩

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!