Django, docker compose, whitenoise and railways: new js files not found in production (it works in local)

I am working in a django project where I have installed and configured whitenoise to serve static files in railways. But railways is not serving or collecting my new js files. In my browser I get a 404

in settings I have configured (theoretically) whitenoise

    INSTALLED_APPS = [
    'app.apps.AppConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'captcha',
    'storages',
    'whitenoise.runserver_nostatic',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

and this is where I am managing my files (I am also using amazon aws s3 to upload pdfs and other files)

STATIC_URL = "static/"
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_ROOT = BASE_DIR / "staticfiles"
STORAGES = {
    "default": {
        "backend": "django.core.files.storage.FileSystemStorage",
    },
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedStaticFilesStorage", # CompressedStaticFilesStorage
    },
}

# Modify storage configuration
if ENV_STATE == "production":
    SECURE_SSL_REDIRECT = True
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

    # HIPAA-compliant Backblaze configuration
    AWS_ACCESS_KEY_ID = os.environ['B2_ACCESS_KEY_ID']
    AWS_SECRET_ACCESS_KEY = os.environ['B2_SECRET_ACCESS_KEY']
    AWS_STORAGE_BUCKET_NAME = os.environ['B2_BUCKET_NAME']
    AWS_S3_ENDPOINT_URL = os.environ['B2_ENDPOINT_URL']
    AWS_S3_REGION_NAME = os.environ.get('B2_REGION_NAME', 'us-west-004')  # Default region
    
    # Security settings
    AWS_DEFAULT_ACL = 'private'  # HIPAA: Private access only
    #AWS_LOCATION = 'protected'  # This should match your prefix
    AWS_S3_FILE_OVERWRITE = False
    AWS_S3_ADDRESSING_STYLE = 'path'
    AWS_QUERYSTRING_AUTH = True  # Enable signed URLs for HIPAA compliance
    AWS_QUERYSTRING_EXPIRE = 3600  # 1-hour URL expiration
    AWS_S3_FILE_OVERWRITE = False  # Prevent overwriting files
    AWS_DEFAULT_ACL = 'private'  # Make files private
    # Checksum configuration  
    AWS_S3_CONFIG = Config(
        signature_version='s3v4',
        s3={
            's3_disable_checksum': True,
            'payload_signing_enabled': True  # Enable payload signing
        }
    )
    
    # Encryption settings
    AWS_S3_OBJECT_PARAMETERS = {
        'ServerSideEncryption': 'AES256',  # Backblaze supports AES-256
    }
    
    # Storage backend configuration
    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
    STORAGES = {
        "default": {
            "BACKEND": "storages.backends.s3.S3Storage",
            "OPTIONS": {
                "client_config": Config(
                    request_checksum_calculation="when_required",
                    response_checksum_validation="when_required",
                ),
                "gzip": True,
                "bucket_name": AWS_STORAGE_BUCKET_NAME,
                "location": "protected/",
                "default_acl": "private",
                "querystring_auth": True,
                "file_overwrite": False,
                "url_protocol": "https:",
            },
        },
        "staticfiles": {
            "BACKEND": "whitenoise.storage.CompressedStaticFilesStorage", # CompressedStaticFilesStorage
        },
    }

I have a start-django.sh file too

echo "----- Collect Static Files -----"

poetry run python manage.py collectstatic --no-input 

# Debug: Verify collected static files
echo "----- Collected Static Files -----"


# Run migrations
echo "----- Running migrations -----"
poetry run python manage.py migrate

# Start server
if [[ "$ENV_STATE" == "production" ]]; then
    echo "----- Starting Gunicorn (Production) -----"
    poetry run gunicorn tcgservices.wsgi --workers $GUNICORN_WORKERS --forwarded-allow-ips='*'
else
    echo "----- Starting Django Dev Server (QA/Dev) -----"
    poetry run python manage.py runserver 0.0.0.0:8000
fi

what is interesting is that in local development, with docker compose I do not have this problem

can you help me please? thanks

Вернуться на верх