使用 Apache 缓存网站图像 [英] Website image caching with Apache

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

问题描述

我怎样才能让 Apache 上的静态内容{被浏览器缓存}而不是{检查新鲜度{每个请求}}?

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

我正在开发一个托管在 Apache 网络服务器上的网站.最近,我正在测试一些带有标题的东西(不同类型内容的 Content-Type),看到了很多对图像的条件请求.示例:

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 Not Modified".这很好(传输的数据更少),但这意味着每次加载页面都会产生 20 多个请求(由于所有这些往返,页面加载时间更长,即使启用了 Keep-Alive 和流水线).

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 方法有效,即使使用 favicon.

the mod_expires method works, even with the favicon.

推荐答案

Expires 模块解决了这个问题

Expires module in Apache solves this

a2enmod expires

需要在server config中加载,并在.htaccess(或server config)中设置.

it needs to be loaded in server config, and set up in .htaccess (or in server config).

使用 Expires 标头,资源是只要求第一次.在到期日期之前,从浏览器缓存中完成后续请求.在指定的时间到期并且需要资源后,才会再次请求它(有条件 - 将返回 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,需要做更多的工作(Apache 通常无法识别 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"

瞧,It Works™!

And voila, It Works™!

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

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