Cache-Control仅适用于媒体文件,而不适用于使用Django中的django-pipeline服务的静态文件

时间:2014-09-13 08:52:00

标签: python django .htaccess cache-control

我试图将Cache-Control用于我的静态文件。以下是.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

知道我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

您正在从django(通过django-pipeline)提供静态文件,但是从Web服务器提供。然后.htaccess指令无效。