与Apache网站形象缓存 [英] Website image caching with Apache

查看:177
本文介绍了与Apache网站形象缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何,我可以得到Apache的静态内容进行{由浏览器缓存}和{没有检查新鲜{每个请求}}?

How can I get static content on Apache to be {cached by browser} and not {checked for freshness {with every request}}?

我正在举办的Apache网络服务器在网站上。最近,我在测试用头的东西(内容类型为不同类型的内容),并看到了很多关于图像有条件的请求。例如:

I'm working on a website hosted on Apache webserver. Recently, I was testing something with headers (Content-Type for different types of content) and saw a lot of conditional requests for images. Example:

200 /index.php?page=1234&action=list
304 /favicon.ico
304 /img/logo.png
304 /img/arrow.png
(etc.)

虽然图像文件是静态的内容和浏览器被缓存,每一个用户打开链接到他们的网页的时候,他们是有条件要求,它们发送304未修改。这是很好的(更少的数据传输),但它意味着20+与每一个页面加载更多的请求(由于所有这些往返较长的页面加载,甚至与保活和流水线启用)。

Although the image files are static content and are cached by the browser, every time an user opens a page that links to them, they are conditionally requested, to which they send "304 Not Modified". That's good (less data transferred), but it means 20+ more requests with every page load (longer page load due to all those round-trips, even with Keep-Alive and pipelining enabled).

我如何告诉浏览器保留现有的文件,而不是检查更新的版本?

How do I tell the browser to keep the existing file and not check for newer version?

编辑:
在指定mod_expires方法的工作,即使有图标。

the mod_expires method works, even with the favicon.

推荐答案

过期的模块Apache的解决了这个 - 它需要在服务器配置被加载,并在的.htaccess 设置(或服务器配置)

Expires module in Apache solves this - it needs to be loaded in server config, and set up in .htaccess (or in server config).

随着过期头,资源只要求在第一时间。在到期日之前,后续请求是从浏览器缓存中实现。在指定的时间过期,需要在资源之后,才把它再次请求(有条件 - 304将是不变的资源返回)。它到期之前从缓存中清除它的唯一可靠方法是手动,或者通过强制刷新(通常按Ctrl-F5)。 (这可能是一个问题,如果该资源在此期间的变化,但静图像不经常改变。)

With an Expires header, the resource is only requested the first time. Before the expiration date, subsequent requests are fulfilled from browser cache. After the specified time expires and the resource is needed, only then it is requested again (conditionally - a 304 will be returned for an unchanged resource). The only reliable way to clear it from the cache before it expires is manually, or by forcing a refresh (usually Ctrl-F5). (This could be an issue if the resource changes in the meantime, but statical images don't change very often.)

# enable the directives - assuming they're not enabled globally
ExpiresActive on

# send an Expires: header for each of these mimetypes (as defined by server)
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# css may change a bit sometimes, so define shorter expiration
ExpiresByType text/css "access plus 1 days"

有关favicon.ico的,需要更多的工作(通常阿帕奇无法识别的Windows图标文件,并将此作为默认文本/纯)。

For favicon.ico, a bit more work is needed (Apache normally does not recognize Windows icon files, and sends this as the default text/plain).

# special MIME type for icons - see http://www.iana.org/assignments/media-types/image/vnd.microsoft.icon
AddType image/vnd.microsoft.icon .ico
# now we have icon MIME type, we can use it
# my favicon doesn't change much
ExpiresByType image/vnd.microsoft.icon "access plus 3 months"

和瞧,它工作的™!

这篇关于与Apache网站形象缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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