针对页面速度优化的 Apache Vhost 指令
我目前在我的虚拟主机中使用此设置:
<Location />
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</Location>
<Directory />
ExpiresActive On
ExpiresByType text/html "access plus 5 minutes"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresDefault "access plus 1 day"
<FilesMatch "\.(ico|jpeg|pdf|flv|jpg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=2592000, public"
Header unset Last-Modified
Header unset ETag
FileETag None
</FilesMatch>
<FilesMatch "\.(html|php)$">
Header set Cache-Control "max-age=900, public, must-revalidate"
</FilesMatch>
</Directory>
虽然它对于加快速度非常有效,但有时用户看不到他们自己对内容所做的更改(主要是在使用 FireFox 时):( 有任何建议/优化提示吗?
I currently use this setup in my vhost:
<Location />
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</Location>
<Directory />
ExpiresActive On
ExpiresByType text/html "access plus 5 minutes"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresDefault "access plus 1 day"
<FilesMatch "\.(ico|jpeg|pdf|flv|jpg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=2592000, public"
Header unset Last-Modified
Header unset ETag
FileETag None
</FilesMatch>
<FilesMatch "\.(html|php)$">
Header set Cache-Control "max-age=900, public, must-revalidate"
</FilesMatch>
</Directory>
While it works great for speeding up the thing, sometimes users dont see the changes they themselfs made on content (mainly while using FireFox) :( any suggestions / optimization hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应强制浏览器缓存,而应发送必须重新验证标头,并通过发送 Expires 和 Last-Modified 标头从编程语言(例如 PHP)内控制缓存。然后,浏览器将向您的网站询问每个请求的最新版本,但如果没有任何更改,请确保以空页面进行答复。
这可能需要一些时间来实施,但它绝对有效。
Instead of forcing the browser to cache, you should send a must-revalidate header and control the caching from within your programming language (for example, PHP) by sending an Expires and Last-Modified header. The browser will then ask your site for the latest version on each request, but make sure to answer with an empty page if nothing has changed.
This may take some time to implement, but it definitely works.
这里的问题是浏览器缓存了你的 javascript、css 和图像,因此不会知道你是否修改了服务器中的任何内容,除非缓存过期。
例如,假设您有一个名为 script.js 的 JS 文件。根据 htaccess 文件中的以下规则,
javascript 文件会缓存一个月,因此除非手动清除缓存,否则 1 个月内不会再请求 JS 文件。
如何解决这个问题。
假设您的文件名为 script.js,在 HTML 文件中,您应该包含 as
或
100 可以是任何数字。我通常从 1、2、3.. 等等(准确地说是版本号)增加 if。
现在,在我的 htaccess 中,我有类似的内容
上述两个条件确保使用实际文件,无论文件名和文件类型之间使用什么数字。
这可以确保每次进行更改时,一旦更改版本号,所有旧缓存都会过期,并且所有用户都会看到最新的代码。
The problem here is browser caches your javascript,css and images and hence won't know if you have modified anything in the server unless the cache expires.
For example, consider you have a JS file named script.js. According to the following rules you have in your htaccess file,
the javascript files are cached for a month and hence no new requests will be asked for the JS files for 1 month unless the cache is manually cleared.
How to solve this problem.
Assuming your file name is script.js, in HTML file, you should include is as
or
100 could be any number. I usually increase if from 1, 2, 3.. and so on (version number to be precise).
Now, in my htaccess, I have something like
The above two conditions ensures that the actual file is used, no matter what number is used between the filename and the filetype.
This ensures that everytime you make a change, once you change the version number, all your old cache expires and all the users see the latest code.
在 Hannes 回复后发表此评论,该问题是基于内容而不是在 js/css 文件中。
我曾经遇到过这个问题,但无法确定问题出在哪里。我认为原因可能是 HTML 页面缓存在我和浏览器之间的代理服务器中的某个位置。
因此,我所做的是使用名为 hash 的查询参数生成 url,该参数类似于 md5(time("U"));
例如,具有相同问题的网址类似于
http://test.com/controller/functionname/< /a>
在此问题之后,我进行了更改,以便用户单击的所有网址都类似于
http:// /test.com/controller/functionname/?hash={something}。
我希望这对你有用。另外为了双重确定,您可以在您的网页中添加。
Posting this comment after the reply from Hannes that the issue is based on content and not in js/css files..
I had this issue once, but couldn't nail down to understand what's the issue. I assumed that the reason could be that the HTML page is cached somewhere in the proxy server in between me and the browser.
So, what I did was generated urls with a query parameter called hash, which had something like md5(time("U"));
For example, the url which had the same problem was something like
http://test.com/controller/functionname/
After this issue, I changed so that all the urls the user clicks is something like
http://test.com/controller/functionname/?hash={something}.
I hope that works out for you. Also to be double sure, you could add in your web page.