优化YSlow add expires headers项目提升博客访问速度

这两天一直对本站进行优化,Gtmetrix 得分实在难看。YSlow 可以分析网站的页面,并告诉你为了提高网站性能,如何基于某些规则而进行优化。这里着重介绍一下优化项中的 add expires headers 。本站网站评分因为这一项评分F而拉低。

从网站评分看起,一图胜千言:
优化YSlow add expires headers项目提升博客访问速度技术教程主机格调

由于 add expires headers 这一项,网站整体评分为F,所以,我必须从这里下手。

add expires headers

首先,add expires headers 到底是什么?

Expires headers tell the browser whether they should request a specific file from the server or whether they should grab it from the browser’s cache.

简单来说:就是向文档添加一个有效期,告诉浏览器这个文档的有效性和持久性。如果已有缓存,文档就可以从浏览器中的缓存(除已经过期)而不是从服务器读取。接着,客户端验证缓存中的副本,看看是否过期或者失效,以决定是否必须从服务器获得更新。

添加该项主要是可以加快网站的访问速度,对于一些不长更新的静态文件,都可以进行设置。如

  • images: jpg, gif, png
  • favicon/ico
  • javascript
  • css

设置方法

那我该如何设置?根据 gtmetrix 中记载:

We define our Expires Headers in the .htaccess file.

格式如下:

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
</IfModule>

但是这个没什么用。我们可以考虑在.htaccess中进行设置。

关于 .htaccess

.htaccess文件提供了针对目录改变配置的方法,即在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录。

.htaccess可以帮我们实现包括:文件夹密码保护、用户自动重定向、自定义错误页面、改变你的文件扩展名、封禁特定IP地址的用户、只允许特定IP地址的用户、禁止目录列表,以及使用其他文件作为index文件等一些功能。

.htaccess文件有以下特点:

1.大部分服务器都支持;

2.将 .htaccess 放置在项目目录下;

3.下级 .htaccess 会覆盖上级 .htaccess;

添加以及配置 .htaccess

开始配置 .htaccess

进入test项目目录:

cd test

创建 .htaccess 文件:

echo '' >> .htaccess

打开 .htaccess 文件:

vim .htaccess

拷贝如下代码:

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 days"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 3 days"
ExpiresByType image/png "access plus 3 days"
ExpiresByType image/jpg "access plus 3 days"
ExpiresByType image/jpeg "access plus 3 days"
# CSS
ExpiresByType text/css "access plus 3 days"
# Javascript
ExpiresByType application/javascript "access plus 3 days”
FileETag none
</IfModule>

保存并退出,搞定~

如果你觉得还是不够用,来看看下 面这个超完整版:

友情提示:配置完,记得重启服务器哦~

FileETag None
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"
    ExpiresByType text/css                              "access plus 1 year"
    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"
    ExpiresByType image/x-icon                          "access plus 1 week"
    ExpiresByType text/x-component                      "access plus 1 month"
    ExpiresByType text/html                             "access plus 0 seconds"
    ExpiresByType application/javascript                "access plus 1 year"
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"
    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 month"
    ExpiresByType image/jpeg                            "access plus 1 month"
    ExpiresByType image/png                             "access plus 1 month"
    ExpiresByType video/mp4                             "access plus 1 month"
    ExpiresByType video/ogg                             "access plus 1 month"
    ExpiresByType video/webm                            "access plus 1 month"
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType application/x-font-ttf                "access plus 1 month"
    ExpiresByType font/opentype                         "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"
</IfModule>

未经允许不得转载:主机格调 » 优化YSlow add expires headers项目提升博客访问速度

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏