Serving Django static images

1. Caddy version (caddy version):

v2.5.0

2. How I run Caddy:

systemctl start caddy

a. System environment:

Linux

c. Service/unit/compose file:

Docker-compose.yml:

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

  backend:
    image: arquimedia-backend
    volumes:
      - ./data/backend:/code/static/images
    depends_on:
      - db
    environment:
    command: gunicorn Arquimedia.wsgi:application --bind 0.0.0.0:8000
    ports:
      - 127.0.0.1:8001:8000

  frontend:
    image: arquimedia-frontend
    restart: always
    depends_on:
      - backend
    command: "nginx -g 'daemon off;'"
    ports:
      - 127.0.0.1:8002:3000

d. My complete Caddyfile or JSON config:

arquimedia.pt, www.arquimedia.pt {
        encode gzip zstd

        handle /api/* {
                uri strip_prefix /api
                file_server <project-path>/data/backend/*

                @notStatic {
                         not path /images/*
                 }

                reverse_proxy @notStatic localhost:8001
        }

        handle {
                reverse_proxy localhost:8002
        }
}

3. The problem I’m having:

I’m trying to get static files on <project-path>/data/backend but the response come as a blank page, either for existing files or non existing.

Please upgrade to v2.5.1

This is invalid syntax. You need to use the root directive to set where Caddy should look for files.

You can use handle_path instead of handle + uri strip_prefix, because handle_path has built-in prefix stripping (if I understand correctly that your images are within /api :thinking: )

I would write your config like this:

arquimedia.pt, www.arquimedia.pt {
	encode gzip zstd

	handle /api/images* {
		uri strip_prefix /api

		root * /project/path/data/backend
		file_server
	}

	handle_path /api/* {
		reverse_proxy localhost:8001
	}

	handle {
		reverse_proxy localhost:8002
	}
}
1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.