File server for flutter (web) alongside reverse proxy

1. Caddy version (caddy version):

v2.5.1 h1:bAWwslD1jNeCzDa+jDCNwb8M3UJ2tPa8UZFFzPVmGKs=

2. How I run Caddy:

Default systemd unit file

a. System environment:

5.15.0-33-generic on DigitalOcean

d. My complete Caddyfile or JSON config:

(/etc/caddy/Caddyfile)

trackstar.ml {
        handle /ws/* {
                reverse_proxy localhost:8080
        }
        handle /web {
                root * /home/server/trackstar/client/build/web
                file_server
        }
}

3. The problem I’m having:

I’m trying to reverse proxy websocket connections (under the /ws/* path, e.g. /ws/3233/jms55) to the backend of the server, which works fine.

I’m also trying to serve a web app built with flutter (under the /web path), which has an index.html + facivon.png + *.js + *.wasm + etc files in the directory /home/server/trackstar/client/build/web of my server.

Going to https://trackstar.ml/web shows an empty page with no console output or anything. I can’t figure out why it isn’t serving the flutter app - I assume my config is wrong somehow. Any help on this would be appreciated!

Hi :wave:

path matchers are exact since Caddy v2.

So

handle /web {

will only match /web but not /web/ or /web/something.else.

handle /web* {

on the other hand, matches /web, /web/ and e.g. /web/something.else (and /webANYTHING actually).


Also, a request to /web/something.else would try to serve the file /home/server/trackstar/client/build/web/web/something.else (note the /web/web/) because handle passes the full path to file_server.
Usually one would use handle_path in your case:

handle_path /web* {

And finally, linux file permissions:

caddy runs as user caddy (and group caddy) with the default systemd service and might not have access to your /home/home/server/trackstar/client/build/web directory and respond blank http/403 (Forbidden) pages.

I explained that a bit in Reverse proxy + static file serving results in 403 (Forbidden) for static files - #2 by IndeedNotJames :slight_smile:

Feel free to ask any further questions :innocent:

1 Like

Thank you for the great explanations!

1 Like