Apache不缓存jpg,但缓存了其他扩展名

时间:2019-07-28 08:42:03

标签: apache caching wamp

我使用以下代码将jpg和其他图像类型缓存在我的.htaccess文件中:

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
    Header set Cache-Control "max-age=3024000, public"
</FilesMatch>

所有图像类型均已缓存,但jpg未缓存

我也使用了以下代码:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 day"
    ExpiresByType image/jpg "access plus 5 days"
    ExpiresByType image/jpeg "access plus 5 days"
    ExpiresByType image/gif "access plus 5 days"
    ExpiresByType image/png "access plus 5 days"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
</IfModule>

但是它也不起作用

所有需要的模块都在httpd.conf中处于活动状态,例如

LoadModule expires_module modules/mod_expires.so
LoadModule file_cache_module modules/mod_file_cache.so
LoadModule cache_module modules/mod_cache.so
LoadModule cache_disk_module modules/mod_cache_disk.so

设置后,我还重新启动了Apache服务器。

我在Windows Server 2016上使用了Wamp服务器

1 个答案:

答案 0 :(得分:0)

我将我的.htaccess文件更改为此代码,现在可以正常工作了

<IfModule mod_deflate.c>
    <FilesMatch "\.(html|php|txt|xml|js|css)$">
        SetOutputFilter DEFLATE
    </FilesMatch>
</IfModule>

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
    Header set Cache-Control "max-age=604800, public "
</FilesMatch>

# BEGIN Expire headers
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 7200 seconds"
 ExpiresByType image/jpg "access plus 2592000 seconds"
 ExpiresByType image/jpeg "access plus 2592000 seconds"
 ExpiresByType image/png "access plus 2592000 seconds"
 ExpiresByType image/gif "access plus 2592000 seconds"
 AddType image/x-icon .ico
 ExpiresByType image/ico "access plus 2592000 seconds"
 ExpiresByType image/icon "access plus 2592000 seconds"
 ExpiresByType image/x-icon "access plus 2592000 seconds"
 ExpiresByType text/css "access plus 2592000 seconds"
 ExpiresByType text/javascript "access plus 2592000 seconds"
 ExpiresByType text/html "access plus 7200 seconds"
 ExpiresByType application/xhtml+xml "access plus 7200 seconds"
 ExpiresByType application/javascript A259200
 ExpiresByType application/x-javascript "access plus 2592000 seconds"
 ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
</IfModule>
# END Expire headers

# BEGIN Cache-Control Headers
<IfModule mod_headers.c>
     <FilesMatch "\.(ico|jpe?g|png|gif|swf|css|gz)$">
        Header set Cache-Control "max-age=2592000, public"
     </FilesMatch>
     <FilesMatch "\.(js)$">
        Header set Cache-Control "max-age=2592000, private"
     </FilesMatch>
     <filesMatch "\.(html|htm)$">
        Header set Cache-Control "max-age=7200, public"
     </filesMatch>
     # Disable caching for scripts and other dynamic files
     <FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
        Header unset Cache-Control
     </FilesMatch>
</IfModule>
# END Cache-Control Headers