본문 바로가기

코딩

settings 보안 및 캐시 설정

728x90
반응형

# settings.py MySQL 설정 예시

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'phrase_db',
        'USER': 'your_user',
        'PASSWORD': 'your_password',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'charset': 'utf8mb4',
            'collation': 'utf8mb4_unicode_ci',
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
            'isolation_level': 'read committed',
            # 연결 풀 설정
            'connect_timeout': 60,
            'read_timeout': 60,
            'write_timeout': 60,
            # SSL 설정 (필요한 경우)
            # 'ssl': {
            #     'ca': '/path/to/ca.pem',
            #     'cert': '/path/to/client-cert.pem',
            #     'key': '/path/to/client-key.pem'
            # }
        },
        # 연결 풀링 설정
        'CONN_MAX_AGE': 600,  # 10분
        'CONN_HEALTH_CHECKS': True,
    }
}

# MySQL 특화 설정
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# 대용량 파일 업로드를 위한 설정
FILE_UPLOAD_MAX_MEMORY_SIZE = 10485760  # 10MB
DATA_UPLOAD_MAX_MEMORY_SIZE = 10485760  # 10MB

# 쿼리 로깅 (개발 환경에서만)
if DEBUG:
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
            },
        },
        'loggers': {
            'django.db.backends': {
                'handlers': ['console'],
                'level': 'DEBUG',
                'propagate': False,
            },
            'phrase.models': {
                'handlers': ['console'],
                'level': 'INFO',
                'propagate': False,
            },
        },
    }

# 캐시 설정 (Redis 권장)
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
            'CONNECTION_POOL_KWARGS': {
                'max_connections': 50,
                'retry_on_timeout': True,
            },
            'COMPRESSOR': 'django_redis.compressors.zlib.ZlibCompressor',
            'IGNORE_EXCEPTIONS': True,
        },
        'KEY_PREFIX': 'phrase',
        'TIMEOUT': 300,  # 기본 5분
    }
}

# 세션 설정
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
SESSION_CACHE_ALIAS = 'default'

# 파일 업로드 설정
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

# 정적 파일 설정
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

# 보안 설정
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'

# CORS 설정 (필요한 경우)
# CORS_ALLOWED_ORIGINS = [
#     "https://example.com",
#     "https://sub.example.com",
# ]

728x90
반응형