Caddy serving static files but not media files on local host

1. The problem I’m having:

I am able to serve staticfiles on local host but I cannot serve media files.

2. Error messages and/or full log output:

Please DO NOT REDACT any information except credentials.

compose exec web ls -l /app/media/rooms/     
total 27616
-rwxr-xr-x 1 root root   239936 Feb 11 23:07 3-KPW-DOUBLE-KILIVIEW-2.jpg
-rwxr-xr-x 1 root root   402145 Jan 22 21:50 DSC_8615_1.jpg
-rwxr-xr-x 1 root root 13814798 Jan 31 16:01 DSC_8615_3Een2SF.JPG
-rwxr-xr-x 1 root root 13814798 Jan 31 16:07 DSC_8615_Vnteo7Z.JPG

3. Caddy version:

alpine

4. How I installed and ran Caddy:

docker-compose up -d
[+] Running 4/4
:heavy_check_mark: Network shimba_default Created 0.1s
:heavy_check_mark: Container django-postgres Started 1.6s
:heavy_check_mark: Container django-app Started 1.8s
:heavy_check_mark: Container django-caddy-app Started

a. System environment:

b. Command:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

c. Service/unit/compose file:

services:
  db:
    image: postgres:latest
    container_name: django-postgres
    restart: always
    environment:
      - POSTGRES_DB=${DB_NAME}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
    env_file:
      - .env
    ports:
      - '5432:5432'
    volumes:
      - pg_data:/var/lib/postgresql/data

  web:
    build: .
    container_name: django-app
    restart: always
    command: gunicorn --bind 0.0.0.0:8000 website.wsgi:application
    ports:
      - "8000:8000"
    environment:
      - DJANGO_DEBUG=${DEBUG}
      - DJANGO_SECRET_KEY=${SECRET_KEY}
      - DJANGO_ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS}
      - DJANGO_CSRF_TRUSTED_ORIGINS=${DJANGO_CSRF_TRUSTED_ORIGINS}
      - DATABASE_ENGINE=${DB_ENGINE}
      - DATABASE_NAME=${DB_NAME}
      - DATABASE_USER=${DB_USER}
      - DATABASE_PASSWORD=${DB_PASSWORD}
      - DATABASE_HOST=${DB_HOST}
      - DATABASE_PORT=${DB_PORT}
    env_file:
      - .env
    depends_on:
      - db
    volumes:
      - ./staticfiles:/app/staticfiles  # Ensure collectstatic stores files here
      - ./media:/app/media 

  caddy:
    image: caddy:alpine
    restart: always
    container_name: django-caddy-app
    ports:
      - "80:80"
      - "443:443" 
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./static:/srv/static  # Static files volume
      - ./media:/srv/media    # Media files volume
      - caddy_data:/data      # SSL certificates storage
      - caddy_config:/config # Caddy configuration
    depends_on:
      - web

volumes:
  pg_data:
  caddy_data:
  caddy_config:

networks:
  app_network:

d. My complete Caddy config:

shimbagreenlodge.net{

    # Enable compression
    encode gzip zstd

    # Serve static and media files correctly
    root * /srv
    file_server

    # Reverse proxy all other requests to Django
    @notStatic {
        not path /static/* /media/*
    }
    reverse_proxy @notStatic web:8000
}

5. Links to relevant resources:

Give it a try with the following:

    @notStatic {
        not path /static/*
        not path /media/*
    }

Caddyfile:

{
	http_port 8080
}

:8080 {

	@notStatic {
		not path /static/*
		not path /media/*
	}

	respond "Static"
	respond @notStatic "Not Static"

}
$ caddy run --config Caddyfile

Test Cases:

$ curl http://localhost:8080
Not Static

$ curl http://localhost:8080/foo
Not Static

$ curl http://localhost:8080/static/foo
Static

$ curl http://localhost:8080/media/foo
Static

Do i make any changes to the docker file?

That’s a change in your Caddyfile.

From this:

@notStatic {
    not path /static/* path /media/*
}

to this:

@notStatic {
    not path /static/*
    not path /media/*
}