过期和CDNS-YSlow问题 [英] Expires and CDNS - YSlow Problems

查看:118
本文介绍了过期和CDNS-YSlow问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我首先提到我已经做了一些挖掘工作,但似乎找不到我想要的正确答案.

我正在使用一些外部资源的网站上工作

I'm working on a site where I am using a few external resources:

  • Google Analytics (//www.google-analytics.com/analytics.js)
  • Latest jQuery version (http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js)
  • Google Fonts (http://fonts.googleapis.com/css?family=Lato:400,300)

我在使用内容传送网络(CDN)"上获得"F"等级.另一个 SO Post 链接到YSlow常见问题解答(直接链接),但这似乎表明我需要在浏览器上定义CDN,这似乎是一种快速的方法修复了我的问题,但这不能解决其他浏览器和/或设备上的问题.

I'm getting an "F" grade on "Use a Content Delivery Network (CDN)". A different SO Post links to the YSlow FAQ (direct link) but this seems to indicate that I need to define my CDN's on my browser, which seems to be a quick fix for me, but this does not solve the problem on other browsers and/or devices.

对于添加过期标头"等级,我得到E.问题是,这就是我得到的:

As for the "Add Expires headers" grade,I get an E. Problem is, this is what I'm getting:

There are 3 static components without a far-future expiration date.

(2014/2/26) http://fonts.googleapis.com/css?...
(2014/2/26) http://www.google-analytics.com/analytics.js
(2014/2/26) http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js

我将如何纠正此问题?我的本地文件全部由我的.htaccess处理,如下所示:

How would I go in order to correct this? My local files are all taken care of by my .htaccess as shown, here:

AddType image/x-icon .ico

ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/ico "access plus 1 year"
ExpiresDefault "access plus 7 days"

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
    # access plus 1 year
    Header set Cache-Control "max-age=31536000"
</FilesMatch>

这样的帖子似乎暗示这是不可能的,或者我调用了一个本地脚本,在该脚本中我使用自己的带宽来加载(并缓存)所需的外部文件.

This SO post seems to suggest it is either impossible, or I call upon a local script where I use my bandwidth to load (and cache) the required external files.

因此,请牢记以下几点:

So, with these things in mind, here is what I'm looking at:

  1. 外部资源应该是CDN的资源,但是YSlow会读取我的本地首选项,这会因用户而异,因此我需要YSlow官方网站提出的about:config解决方案的替代方案.
  2. 过期时间是由文件所在的服务器设置的,因此我似乎无权定义它的过期值.因此,从我收集到的信息来看,我可以将这些文件携带到服务器上,或者使用本地PHP文件并从位于服务器上的上述PHP文件进行缓存,但这似乎并不是最佳选择.有办法解决这个问题吗?
  1. External ressources should be CDN's, but YSlow reads my local preferences which will vary from user to user, so I need an alternative to the about:config solution proposed by the YSlow official site.
  2. Expires are set by the server where the file is, so I don't seem to have access to defining it's expiration values. So, from what I gather, I could carry these files onto my server or use a local PHP file and cache it from said PHP file located my server, but this doesn't seem optimal. Is there a way to go around this?

推荐答案

简短答案:

当您从这样的外部服务器加载资源时,您无法控制太多,但对于您的站点来说仍然可以.当使用诸如YSlow之类的工具时,重要的是不要让所有内容都陷入"A"级.最好了解该工具给您的提示,并据此做出决定.

You can't control much when you're loading resources from external servers like that, but it may still be okay for your site. When using a tool like YSlow it's important to not get caught up into an "A" grade for everything. It's best to understand the hints the tool is giving you, and make decisions accordingly.

更长的答案:

内容交付网络

YSlow将报告其未被识别为CDN的任何域的较低等级.您可以添加CDN主机名,这样可以反映出更好的成绩.我将添加fonts.googleapis.comajax.googleapis.com,因为它们的行为都类似于CDN.可以将其视为在YSlow副本中保存首选项.在此处添加域实际上并不会改变您的效果,只会提高您的得分.

YSlow will report lower grades for any domains it doesn't recognize as a CDN. You can add CDN hostnames, then it will reflect a better grade. I would add fonts.googleapis.com and ajax.googleapis.com because both behave like CDNs. Think of it as saving a preference in your copy of YSlow. Adding the domains here doesn't actually change your performance, it just improves your score.

过期标题

您无法控制要从其他站点加载的内容的到期标头.您可以考虑的选项:

You can't control expires headers for content you're loading from a different site. Options you could consider:

  1. 继续使用外部网站;在这种情况下,它们表现得很好,您的性能可能会很好.只需忽略YSlow给您的低分即可.
  2. 在本地托管字体和jQuery文件,然后如.htaccess中所示设置远距离过期标头.这样可以为您提供更好的YSlow等级,但是如果您的服务器比CDN慢,则实际性能可能会更差. (请参阅此 SO问题)
  1. Continue using external sites; in this case these are pretty well behaved and your performance will probably be pretty good. Just ignore the low grade that YSlow is giving you.
  2. Host your fonts and jQuery files locally, then set far-futures expires headers as you've shown in your .htaccess. This would give you a better YSlow grade, but actual performance may be worse if your server is slower than the CDNs. (see this SO question)
  3. Use external sites but adjust your URL; in the case of jQuery, if you specify on more version level (1.8.0 instead of 1.8) you will get a far future expires header (see this SO question)

这篇关于过期和CDNS-YSlow问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆