缓存控制只适用于媒体文件,而不适用于django中使用django管道服务的静态文件



我正在尝试对静态文件使用缓存控制。以下是我的.htaccess文件代码

# 1 YEAR
<FilesMatch ".(ico|svg|woff|eot|ttf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
# 1 WEEK
<FilesMatch ".(jpg|png|gif|css|js)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

我正在使用Django Pipeline来压缩我的JS和CSS文件。以下是我的相关settings.py

MEDIA_ROOT = '/home/jaskaran/edmhunters/media'
MEDIA_URL = '/media/'
STATIC_ROOT = '/home/jaskaran/edmhunters/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    root('hunt/static'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

使用redbot.org测试媒体文件URL会得到类似以下的结果

HTTP/1.1 200 OK
    Date: Sat, 13 Sep 2014 08:46:35 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Wed, 10 Sep 2014 17:57:24 GMT
    ETag: "4b6d-502b9c8c3966e"
    Accept-Ranges: bytes
    Content-Length: 19309
    Cache-Control: max-age=604800, public
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: image/jpeg

对于静态文件,这是它输出的

HTTP/1.1 200 OK
    Date: Sat, 13 Sep 2014 08:49:12 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Tue, 26 Aug 2014 05:43:32 GMT
    ETag: 1409031812.69
    Content-Length: 23907
    Keep-Alive: timeout=5, max=99
    Connection: Keep-Alive
    Content-Type: image/png

知道我错过了什么吗?

您从django(通过django管道)但从web服务器提供静态文件。那么.htaccess指令无效。

最新更新