0
votes

I'm having a weird issue with my Django app deployed in Heroku. I'm using django-herokuapp with an AmazonS3 bucket and django-heroku-memcacheify for the cache. My production settings look like:

from .base import *
import dj_database_url
import os
from memcacheify import memcacheify


INSTALLED_APPS += (
   'herokuapp',
)

# Heroku platform settings.
HEROKU_APP_NAME = "myapp"
HEROKU_BUILDPACK_URL = "https://github.com/heroku/heroku-buildpack-python.git"
SITE_DOMAIN = "myapp.herokuapp.com"

# Parse database configuration from $DATABASE_URL
DATABASES['default'] = dj_database_url.config()
env = os.environ.copy()
SECRET_KEY = env['SECRET_KEY']

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
TEMPLATE_DEBUG = False

BASE_DIR = os.path.abspath(os.path.dirname(__name__))
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

# Use Amazon S3 for storage for uploaded media files.
DEFAULT_FILE_STORAGE = "storages.backends.s3boto.S3BotoStorage"

# Use Amazon S3 for static files storage.
STATICFILES_STORAGE = "require_s3.storage.OptimizedCachedStaticFilesStorage"
STATIC_ROOT = 'staticfiles'

# Amazon S3 settings.
AWS_ACCESS_KEY_ID = env['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = env['AWS_SECRET_ACCESS_KEY']
AWS_STORAGE_BUCKET_NAME = env['AWS_STORAGE_BUCKET_NAME']
AWS_AUTO_CREATE_BUCKET = True
AWS_HEADERS = {
    "Cache-Control": "public, max-age=86400",
}
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
COMPRESS_URL = STATIC_URL
MEDIA_URL = "https://%s/media/" % AWS_S3_CUSTOM_DOMAIN

AWS_S3_FILE_OVERWRITE = True
AWS_QUERYSTRING_AUTH = False
AWS_S3_SECURE_URLS = True
AWS_REDUCED_REDUNDANCY = True
AWS_PRELOAD_METADATA = True
AWS_IS_GZIPPED = False

CACHES = memcacheify()

# Compress static files offline
COMPRESS_CSS_FILTERS = [
    'compressor.filters.css_default.CssAbsoluteFilter',
    'compressor.filters.cssmin.CSSMinFilter',
]
COMPRESS_CSS_HASHING_METHOD = 'content'
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_STORAGE = STATICFILES_STORAGE
COMPRESS_OFFLINE = True
COMPRESS_ENABLED = True

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "level": "INFO",
            "class": "logging.StreamHandler",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console"],
        }
    }
}

WAGTAILSEARCH_BACKENDS = {
    'default': {
        'BACKEND': 'wagtail.wagtailsearch.backends.db',
        'AUTO_UPDATE': True,
    }
}

# Use the cached template loader
TEMPLATE_LOADERS = (
    ('django.template.loaders.cached.Loader', (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    )),
)


MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
) + MIDDLEWARE_CLASSES + (
    'django.middleware.cache.FetchFromCacheMiddleware',
)

# Excluding logged in (admin) requests
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True

App deployment works just fine, statics are generated and transferred to the bucket without any problem. But when loading the app all the requests to serve static cached files (css+js) look like:

https://my-bucket-name.s3.amazonaws.com/CACHE/css/f8e3b9a96f8f.f8e3b9a96f8f.css

Somehow the name of the file is specified twice in the request. I have no clue why is this happening. The file named f8e3b9a96f8f.css indeed exists in the CACHE/css folder of the amazon bucket.

Any clues anyone?

1

1 Answers

1
votes

Oh well, in case someone runs into the same issue I managed to have everything working fine just by changing the STATICFILES_STORAGEin the settings.py like:

STATICFILES_STORAGE = "storages.backends.s3boto.S3BotoStorage"