Django getting 404 error when trying to load files from MinIO on the same server

I have setup a Django web app (Django version 5.0.6) on an Ubuntu 22.04 LTS operating system with its default backend file system running MinIO 2024-08-03T04-33-23Z. The services are all running using HTTPS. My Django web app is configured to run with Gunicorn version 22.0.0 and Nginx version 1.26.2

My Django settings are provided below.

ALLOWED_HOSTS = [
       'localhost',
       '127.0.0.1',
       [subdomain_ip],
       'subdomain.domain.org'
]

CSRF_TRUSTED_ORIGINS = [f'https://{host}' for host in ALLOWED_HOSTS]
CSRF_ALLOWED_ORIGINS = [f'https://{host}' for host in ALLOWED_HOSTS]
CORS_ORIGINS_WHITELIST = [f'https://{host}' for host in ALLOWED_HOSTS]



INTERNAL_IPS = ['127.0.0.1', 'localhost', [subdomain_ip], 'subdomain.domain.org']

# MINIO BACKEND SETTINGS
MINIO_CONSISTENCY_CHECK_ON_START = True
MINIO_ENDPOINT = 'subdomain.domain.org:9000' 

MINIO_REGION = 'eu-west-1'  # Default is set to None, current is Ireland
MINIO_ACCESS_KEY = '[REDACTED]'
MINIO_SECRET_KEY = '[REDACTED]'
MINIO_USE_HTTPS = True

MINIO_EXTERNAL_ENDPOINT = 'subdomain.domain.org:9000' 
MINIO_EXTERNAL_ENDPOINT_USE_HTTPS = True  

MINIO_STORAGE_ENDPOINT = 'subdomain.domain.org:9000' 
MINIO_STORAGE_ACCESS_KEY = '[REDACTED]' 
MINIO_STORAGE_SECRET_KEY = '[REDACTED]' 

#MINIO_STATIC_FILES_BUCKET = 'bridge-static-files-bucket'
MINIO_MEDIA_FILES_BUCKET = 'bridge-media-files-bucket'


# Media files storage configuration
#DEFAULT_FILE_STORAGE = 'minio_storage.storage.MinioMediaStorage'
DEFAULT_FILE_STORAGE = 'django_minio_backend.models.MinioBackend'
MINIO_STORAGE_MEDIA_BUCKET_NAME = MINIO_MEDIA_FILES_BUCKET
MINIO_STORAGE_MEDIA_URL = f"{MINIO_EXTERNAL_ENDPOINT}/{MINIO_MEDIA_FILES_BUCKET}"

MINIO_PRIVATE_BUCKETS = ['django-backend-dev-private', ]
MINIO_PUBLIC_BUCKETS = ['django-backend-dev-public', ]

#MINIO_PRIVATE_BUCKETS.append(MINIO_STATIC_FILES_BUCKET)
MINIO_PRIVATE_BUCKETS.append(MINIO_MEDIA_FILES_BUCKET)

# Default: True // Creates bucket if missing, then save
MINIO_BUCKET_CHECK_ON_SAVE = True


# SSL
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000  # 1 year
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

With the above config, when using the web app, I can upload the files to MinIO both in the intranet (within the host network) and over the internet. However, while I can preview the uploaded files within the webapp when using the intranet (i.e.host network), I get a 404 Not Found error when I try to preview the uploaded file when accessing the webapp over the internet with the URL below. The firewall has been configured to allow traffic from internet on port 9000 and 9001.

 [error] 990#990: *40 open() "/usr/share/nginx/html/bridge-media-files-bucket/documents/ITF.pdf" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /bridge-media-files-bucket/documents/ITF.pdf?X-Amz-Algorithm=[Redacted]Credential=[Redacted]&X-Amz-Date=20241223T084407Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=[Redacted] HTTP/1.0", host: "subdomain.domain.org"

How do I resolve the 404 error?

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