Caddy 2 and django cannot have /static/ and /media/ served by file_server directive

1. My Caddy version (caddy version):

v2.0.0-rc.3

2. How I run Caddy:

I run caddy via a systemd service on raspberry 3B+.
caddy.service:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=caddy
Group=caddy
WorkingDirectory=/home/pi/stl_viewer
ExecStart=/usr/local/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/local/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

a. System environment:

running on rasp on latest raspbian using systemd

d. My complete Caddyfile or JSON config:

encode gzip
root * /home/pi/stl_viewer/website/static/
file_server
}

ampotstl.ddns.net/media/ {
encode gzip
root * /home/pi/stl_viewer/website/media/
file_server
}

ampotstl.ddns.net {

reverse_proxy localhost:8000
}

3. The problem I’m having:

The reverse proxy works perfectly but I cannot managed to have the static and media file served by caddy.

4. Error messages and/or full log output:

I don’t see anything related to the file server in the log (from journalctl -u caddy)

5. What I already tried:

I tried a simple caddy file just serving /static but that is still not working. I executed it in stl_viewer/website hopping to serve stl_viewer/website/static

ampotstl.ddns.net
file_server /static/* 

I tried running it via command line instead of systemd to be sure the access to /home/pi is not a problem.

the following commands works (without using a caddy file): caddy file-server -domain ampotstl.ddns.net

I also tried this CaddyFile:


encode gzip zstd


file_server /static/*
file_server /media/*

reverse_proxy localhost:8000

Same results, rever-proxy is fine but no static, no media

I made some progress with the following caddyfile:

ampotstl.ddns.net

encode gzip zstd

root * /home/pi/stl_viewer/website/
file_server /static/*

file_server /media/*

reverse_proxy not {/static/*} localhost:8000

The reverse works, static and media are not but I have errors in log now:
http.log.error making dial info: upstream not:: invalid dial address not:: invalid start port: strconv.ParseUint: parsing "": invalid syntax {"request": {"method": "GET", "uri": "/static/", "proto": "HTTP/2.0", "remote_addr": "192.168.1.1:34968", "host": "ampotstl.ddns.net", "headers": {"Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"], "Accept-Language": ["en-US,en;q=0.5"], "Accept-Encoding": ["gzip, deflate, br"], "Cookie": ["csrftoken=x8woAyy9d5QvQ7QsXEDLiMBflArLOMxHituazQvXP2PdOw9gVWdbZwn9ydPAx2cr; sessionid=enbmdo3yykekz8gl69u3s08ka51hpclj"], "Upgrade-Insecure-Requests": ["1"], "Cache-Control": ["max-age=0"]}, "tls": {"resumed": false, "version": 772, "ciphersuite": 4867, "proto": "h2", "proto_mutual": true, "server_name": "ampotstl.ddns.net"}}}

You’re looking for something like this:

ampotstl.ddns.net

encode gzip zstd

root * /home/pi/stl_viewer/website/

@notStatic {
  not path /static/* /media/*
}
reverse_proxy @notStatic localhost:8000

file_server

This will match requests to anything that isn’t /media/* or /static/* and proxy those, and anything else will fall through to file_server.

I suggest you take another look at the matcher syntax documentation:

1 Like

That works, thank you for your help. I will look at the matcher syntax.

1 Like

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