我看过的所有过期标题文章都或多或少地提供了以下解决方案:
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
Run Code Online (Sandbox Code Playgroud)
但它对我没有意义,因为我知道我的哪些图像会改变,哪些不会改变,所以我希望能够为特定的图像文件添加特定的到期日期.我该怎么做?
我知道我可以使用mod_expires添加expires头.但是,如果Apache服务器没有安装mod_expires并且我不想通过像PHP这样的脚本语言来访问文件,我该怎么办?
Apache正在为3 ##状态代码发送Cache-Control标头,例如302重定向.这导致Firefox(可能从Firefox 5开始)缓存302重定向 - 这导致我的某些页面无限重定向循环.
以下是我在我使用的设置httpd.conf:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A600
</IfModule>
Run Code Online (Sandbox Code Playgroud)
如果我删除了ExpiresDefault设置,问题就会消失,所以我确信这是造成问题的确切设置.如果我改变ExpiresDefault A600对ExpiresByType text/html A600这个问题仍然存在.
我希望默认情况下让浏览器缓存我的内容,但这是一个交易破坏者.
是否有任何设置可以告诉Apache为3 ##状态代码发送不同的Cache-Control标头?
我的PHP代码:
$expires_date = date('D, j F Y H:i:s', strtotime('now + 10 years')) . ' GMT';
header("Expires: $expires_date");
header('Content-type: text/javascript');
echo 'hello world';
Run Code Online (Sandbox Code Playgroud)
当我检查响应头时,我看到:
Expires:Thu, 01 Jan 1970 00:00:00 GMT
我究竟做错了什么?
更新:
只是在尝试,但似乎我甚至无法取消Expires via header_remove('Expires');.我仍然看到1970年的约会.
更新:
我的回复标题:
Cache-Control:private
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:74
Content-Type:text/javascript
Date:Wed, 17 Oct 2012 22:40:45 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.2.21 (Win32) PHP/5.3.9
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.9
Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么< 10 KB我正在处理的网站中的普通旧JPEG图像(大约十几个)不会采用缓存控制标头,而是强制输入..htaccess我正在使用的规则基于最新的HTML5 Boilerplate .htaccess文件(下面的相关部分).有什么奇怪的是,网站中有大量相同数量的PNG,除了其中两个外,缓存控制标头工作正常.
<IfModule mod_expires.c>
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"
...
# media: images, video, audio
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"
...
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
Run Code Online (Sandbox Code Playgroud)
(省略号表示ExpiresByType删除了多余的规则部分)
我已经从我的/etc/apache2/apache2.conf文件直接到那里包含的几乎所有conf文件,并且找不到合理的解释.我已经摆弄,Pagesped和Chrome审核了几个小时的死亡,我仍然无能为力.
编辑:如下所述,.htaccess此网站的Web根目录中只有一个文件.JPEG正在通过mime类型image/jpeg正常,但缓存控制标头丢失,我无法弄清楚为什么.
我已更新我的.htaccess文件以包含以下内容:
<FilesMatch "\.(css|js)$">
ExpiresDefault "access plus 1 month"
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)
我试图告诉客户端所有的css和js都不会改变1个月,所以他们可以缓存他们的文件.
我有2个问题,
从添加我现在得到500错误,整个.htaccess文件如下:
<FilesMatch "*\.(css|js)$">
ExpiresDefault "access plus 1 month"
</FilesMatch>
# BEGIN Compression (DEFLATE)
<IfModule mod_deflate.c>
# Enable compression
AddOutputFilterByType DEFLATE text/css text/javascript application/x-javascript text/html ``text/plain text/xml image/x-icon
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
# Make sure proxies deliver correct content
Header append Vary User-Agent env=!dont-vary
# Ensure proxies deliver compressed content correctly
Header append Vary …Run Code Online (Sandbox Code Playgroud)出于CDN失效的目的,我需要在站点URL的路径元素中添加前缀.每当发布新版本的资产时,都会更改此信息.
然后使用mod_rewrite将URL重写为:http://example.com/cdn/20111030/images/image.jpg到http://example.com/images/image.jpg,这是资产实际所在的位置.
我想在响应中添加长期到期标题(至少3个月)(对于文件系统中实际不存在的第一个URL).有谁知道如何做到这一点?
我无法让JS文件上的过期标头正常工作..它可以正常工作,例如CSS文件!?
谁能告诉我我做错了什么?:(
ExpiresActive on
ExpiresDefault A0
ExpiresByType image/gif A29030400
ExpiresByType image/jpeg A29030400
ExpiresByType image/png A29030400
ExpiresByType text/css A29030400
ExpiresByType text/javascript A29030400
ExpiresByType application/x-javascript A29030400
Run Code Online (Sandbox Code Playgroud)
Cache-Control max-age=29030400
Connection Keep-Alive
Date Sun, 14 Oct 2012 12:06:45 GMT
Etag "be-4cbcf2ad48b40"
Expires Sun, 15 Sep 2013 12:06:45 GMT
Keep-Alive timeout=5, max=97
Server Apache/2.4.2 (Win64) PHP/5.4.3
Run Code Online (Sandbox Code Playgroud)
Cache-Control max-age=0
Connection Keep-Alive
Date Sun, 14 Oct 2012 12:06:45 GMT
Etag "1ad-4cc00d069f493"
Expires Sun, 14 Oct 2012 12:06:45 GMT
Keep-Alive timeout=5, max=99
Server Apache/2.4.2 (Win64) …Run Code Online (Sandbox Code Playgroud) 我最近在支持 Apache2 的 VPS 上部署了一个网站,并且正在研究 YSlow 的性能改进建议。这些改进包括使用 mod_deflate 和 mod_expires,由于我有 root 访问权限,我可以直接编辑 Apache 配置文件。
mod_deflate 现在正在工作,但我遇到了 mod_expires 的问题,所以这是我所做的:
启用过期模块并重新启动服务器
a2enmod expires
service apache2 restart
Run Code Online (Sandbox Code Playgroud)
在 mods_enabled 下创建一个新文件“expires.conf”,其中包含以下内容:
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
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"
# CSS
ExpiresByType …Run Code Online (Sandbox Code Playgroud) 嘿!
/static我的Apache 2.x服务器webroot中有一个文件夹.如果请求匹配
/static/<somename like [\S-_]+>.(png|jpg|css|js)/\d{8,15}
Run Code Online (Sandbox Code Playgroud)
例如
/static/bg.jpg/1335455634
Run Code Online (Sandbox Code Playgroud)
我想要两件事:
/static/bg.jpg(摆脱时间戳)如果请求不匹配,请求及其标题应该正常,不重写.理想情况下,/ static/*之外的任何请求都不应受到影响(尽管«巧合的尾随时间段»应该很少......)
我对FilesMatch/RewriteCond只有麻烦,所以我宁愿不发布我的不良尝试...(在我的机器上启用了genereal中的重写,我确实拥有发送缓存相关头文件的权限)
Dankeschön!
mod-expires ×10
.htaccess ×8
apache ×8
apache2 ×1
cdn ×1
header ×1
http-headers ×1
mod-headers ×1
mod-rewrite ×1
redirect ×1
web-hosting ×1