apache 为什么不是我的 javascript 和 css 缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/841209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:34:57  来源:igfitidea点击:

Why isn't my javascript & css caching?

apachehttp.htaccesscachingetag

提问by

It's appears ONLY javascript and css are not caching ... but images are caching.

似乎只有 javascript 和 css 没有缓存......但图像正在缓存。

I am using Firebug and when I refresh the page, I notice in Firebug a lot of 200 HTTP responsesfor js/css but am receiving 304 HTTP codes(content not modified) for all of my images. So it appears that my JS and CSS are not caching.

我正在使用 Firebug,当我刷新页面时,我注意到 Firebug 中有很多200 个针对 js/css 的HTTP 响应,但我收到了所有图像的304 个 HTTP 代码(内容未修改)。所以看起来我的 JS 和 CSS 没有缓存。

Also, when using YSlow to help determine the problem with my JS/CSS content not caching, it informs me that:

此外,当使用 YSlow 帮助确定我的 JS/CSS 内容未缓存的问题时,它会通知我:

There are 4 components with misconfigured ETags

有 4 个组件的 ETag 配置错误

Listed below is my .htaccess file

下面列出的是我的 .htaccess 文件

Options -Indexes
Options +FollowSymLinks  

# Enable ETag
FileETag MTime Size

# Set expiration header
ExpiresActive on
ExpiresDefault "access plus 1 week"

# Compress some text file types
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript text/javascript application/javascript application/json

# Deactivate compression for buggy browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# Set header information for proxies
Header append Vary User-Agent

Any idea what's wrong with my .htaccess access file preventing it from caching my CSS or JavaScript?

知道我的 .htaccess 访问文件有什么问题阻止它缓存我的 CSS 或 JavaScript?

回答by Andries Louw Wolthuizen

Please, consider disablingETag's!

请考虑禁用ETag!

Consider the following settings:

考虑以下设置:

Header unset ETag
FileETag None
Header set Cache-Control "max-age=2678400"

The first two rules disable ETag's completely, so the browser is somewhat forced to listen to the Cache-Control header. The last rule tells the browser to cache the file 2678400 seconds, or 1 month. Tweak the settings to what suits you the most. And apply this configuration on your dir which contains the static files (by, for example, placing an .htaccess file in that dir)

前两条规则完全禁用 ETag,因此浏览器在某种程度上被迫侦听 Cache-Control 标头。最后一条规则告诉浏览器将文件缓存 2678400 秒,或 1 个月。将设置调整为最适合您的设置。并将此配置应用于包含静态文件的目录(例如,通过在该目录中放置一个 .htaccess 文件)

Optional, if your using multiple servers to serve static content, and/or are not sure about the last-modified times those servers report, consider using:

可选,如果您使用多个服务器来提供静态内容,和/或不确定这些服务器报告的最后修改时间,请考虑使用:

Header unset Last-Modified

It tells Apache to not serve any Last-Modified headers, so browsers can only listen to the Cache-Control max-age header.

它告诉 Apache 不提供任何 Last-Modified 标头,因此浏览器只能侦听 Cache-Control max-age 标头。

This settings are used by myself on lots of hightraffic websites, and disabling of ETag's and Last-Modified headers sure helped to drive traffic down to one fifth of what it used to be. Especially Internet Explorer is very sensitive to those settings.

我自己在许多高流量网站上使用了此设置,禁用 ETag 和 Last-Modified 标头确实有助于将流量降低到过去的五分之一。尤其是 Internet Explorer 对这些设置非常敏感。

Be warned:Disabling Last-Modified will stop browsers from asking 304 Content Not Modified requests. In my experience this is positive, because the webserver has less requests to process, and browsers rely more on the Cache-Control settings you serve. But it may or may not suit you. Some browsers will try to validate assets every few minutes if you serve them a "Last-Modified" header, and that's why I would advice to disable the use of it completly.

请注意:禁用 Last-Modified 将阻止浏览器询问 304 Content Not Modified 请求。根据我的经验,这是积极的,因为网络服务器要处理的请求较少,而浏览器更多地依赖于您提供的缓存控制设置。但它可能适合也可能不适合你。如果您为它们提供“Last-Modified”标头,某些浏览器将每隔几分钟尝试验证一次资产,这就是为什么我建议完全禁用它的原因。

Oh, and if you're not sure about your caching; use http://www.redbot.org/to test your assets, it tells you quickly what your headers mean to a browser, and how to interpret different cache-control settings you use.

哦,如果你不确定你的缓存;使用http://www.redbot.org/来测试您的资产,它会快速告诉您标题对浏览器的意义,以及如何解释您使用的不同缓存控制设置。

回答by jspash

YSlow reports misconfigured etags if they don't adhere to a certain pattern. Since you're compressing the css and js, the etags are being output something like this:

如果它们不遵守特定模式,YSlow 会报告错误配置的 etag。由于您正在压缩 css 和 js,因此 etags 将输出如下内容:

Etag "1e10-4889909861a80"-gzip

See the -gzip at the end? It's put there by apache (version 2 only). That is what's causing the "error". YSlow expects to see something like this:

看到最后的 -gzip 了吗?它是由 apache 放在那里的(仅限第 2 版)。这就是导致“错误”的原因。YSlow 希望看到这样的事情:

Etag "xxxx-xxxxxxxxxxxxx"

Bascially, you can't fix this because it's not broken. So don't go crazy trying to get a perfect score if you don't know what your doing. Even that yahoo home page only gets a 90.

基本上,您无法修复此问题,因为它没有损坏。因此,如果您不知道自己在做什么,请不要发疯地试图获得完美的分数。即使是雅虎主页也只有 90。

回答by Kip

This YSlow error message is extremely misleading!

此 YSlow 错误消息极具误导性!

YSlow is actuallycomplaining that you are using ETags at all!

YSlow实际上抱怨您根本就在使用 ETag

YSlow runs in your browser--it has no way of knowing if ETags are configured correctly or not. As a rule of thumb, it is saying that you shouldn't use ETags because you are more likely to have them misconfigured than properly configured in a multi-server environment. (And YSlow is aimed at users with large, multi-server websites.)

YSlow 在您的浏览器中运行——它无法知道 ETag 是否配置正确。根据经验,您不应该使用 ETag,因为与在多服务器环境中正确配置相比,您更可能错误配置它们。(而 YSlow 面向拥有大型多服务器网站的用户。)

Of course, if you're on a single-server setup, or if you're on a distributed server setup but know what you're doing, then ETags are just fine. But YSlow has no way of knowing this.

当然,如果您使用单服务器设置,或者如果您使用分布式服务器设置但知道自己在做什么,那么 ETag 就可以了。但是 YSlow 无法知道这一点。

There is lots of discussion of this in the comments of the error description page that you should check out: http://developer.yahoo.net/blog/archives/2007/07/high_performanc_11.html

在您应该查看的错误描述页面的评论中有很多关于此的讨论:http: //developer.yahoo.net/blog/archives/2007/07/high_performanc_11.html

Also I found this answer on ServerFault that reiterates the point: https://serverfault.com/questions/55919/yslow-says-etags-are-misconfigured-how-to-configure-etags-properly-on-iis7

我也在 ServerFault 上找到了这个答案,重申了这一点:https: //serverfault.com/questions/55919/yslow-says-etags-are-misconfigured-how-to-configure-etags-properly-on-iis7

回答by stepancheg

Yes, it is correct and well-known behavior (maybe not really needed).

是的,这是正确且众所周知的行为(可能不是真的需要)。

Read http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.htmlabout ETag.

阅读http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html关于 ETag。

Probably you want to just disable ETag on server.

可能您只想在服务器上禁用 ETag。

Edit: Also, use LiveHTTPHeaders addon to understand, what your browser does. It works better than FireBug for this task.

编辑:另外,使用 LiveHTTPHeaders 插件来了解您的浏览器的作用。对于这项任务,它比 FireBug 更有效。

回答by stepancheg

I experiences the same problem as you. Removing the etag will work.

我遇到了和你一样的问题。删除 etag 将起作用。

Add the following in the config file: FileETag none

在配置文件中添加以下内容: FileETag none

回答by Ben

Hi I had the same trouble. But just putting in FileETag none didn't work

嗨,我遇到了同样的麻烦。但只是放入 FileETag 没有用

The way I fixed it (and I don't know if this is correct - but it works) was I put the

我修复它的方式(我不知道这是否正确 - 但它有效)是我把

FileETag none

FileETag 无

at the bottom of my htaccess file.

在我的 htaccess 文件的底部。

Then ySlow was happy.

然后ySlow很高兴。