V2 hard to make it right

1. Caddy version (caddy version):

v2.4.3 h1:Y1FaV2N4WO3rBqxSYA8UZsZTQdN+PwcoOcAiZTM8C0I=

2. How I run Caddy:

using a Makefile make reverseproxy

a. System environment:

Linux G4560 5.4.0-77-lowlatency #86-Ubuntu SMP PREEMPT Thu Jun 17 03:26:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

b. Command:

using make reverseproxy

the Makefile:
apiserver:
        air -c air.toml

webclient:
        sudo chown -R `whoami` /usr/lib/node_modules/
        cd svelte/src/vite-plugin-mpa && npm install && npm run build
        cd svelte && npm install && npm run dev

reverseproxy:
        caddy fmt -overwrite Caddyfile
        caddy run -config Caddyfile

d. My complete Caddyfile or JSON config:

{
        debug
        auto_https off
        log {
                output stdout
                level DEBUG
        }
        local_certs
}

127.0.0.1:80 {
        file_server /upload/* browse {
                root ./svelte/dist
        }
        reverse_proxy * 127.0.0.1:3000
        reverse_proxy /api/* 127.0.0.1:9090
}

localhost:80 {
        file_server /upload/* browse {
                root ./svelte/dist
        }
        reverse_proxy * 127.0.0.1:3000
        reverse_proxy /api/* 127.0.0.1:9090
}

3. The problem I’m having:

when visiting localhost it cannot access
upload/anyfile β†’ file server didn’t work
/api/* works fine (2nd reverse_proxy line)
/* didn’t work (1st reverse_proxy line, eg. index.html or apis/index.html or index/index.html, but localhost:3000 works)

the project on ./svelte/ directory is nearly similar to GitHub - kokizzu/svelte-mpa

the directory structure

β”œβ”€β”€ svelte
β”‚   β”œβ”€β”€ dist
β”‚   β”‚   β”œβ”€β”€ apis
β”‚   β”‚   β”‚   └── index.html
β”‚   β”‚   β”œβ”€β”€ assets
β”‚   β”‚   β”‚   β”œβ”€β”€ apis.f873b16e.js
β”‚   β”‚   β”‚   β”œβ”€β”€ index.36259e27.css
β”‚   β”‚   β”‚   β”œβ”€β”€ index.9e9f050f.js
β”‚   β”‚   β”‚   β”œβ”€β”€ main.6a8d7b97.js
β”‚   β”‚   β”‚   β”œβ”€β”€ main.842b04ae.css
β”‚   β”‚   β”‚   β”œβ”€β”€ modulepreload-polyfill.b7f2da20.js
β”‚   β”‚   β”‚   β”œβ”€β”€ root.d86d93ea.js
β”‚   β”‚   β”‚   β”œβ”€β”€ svelte.d72399d3.png
β”‚   β”‚   β”‚   └── vendor.905fb7d8.js
β”‚   β”‚   β”œβ”€β”€ favicon.ico
β”‚   β”‚   β”œβ”€β”€ index
β”‚   β”‚   β”‚   └── index.html
β”‚   β”‚   β”œβ”€β”€ index.html
β”‚   β”‚   └── upload
β”‚   β”‚       └── dummy.txt
β”‚   β”œβ”€β”€ src
β”‚   β”‚   β”œβ”€β”€ assets
β”‚   β”‚   β”‚   └── svelte.png
β”‚   β”‚   β”œβ”€β”€ components
β”‚   β”‚   β”‚   └── Counter.svelte
β”‚   β”‚   β”œβ”€β”€ global.d.ts
β”‚   β”‚   β”œβ”€β”€ pages --> these served normally when using localhost:3000
β”‚   β”‚   β”‚   β”œβ”€β”€ api.js
β”‚   β”‚   β”‚   β”œβ”€β”€ apis
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ App.svelte
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ index.html
β”‚   β”‚   β”‚   β”‚   └── main.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ App.svelte
β”‚   β”‚   β”‚   β”œβ”€β”€ index
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ App.svelte
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ index.html
β”‚   β”‚   β”‚   β”‚   └── main.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ index.html
β”‚   β”‚   β”‚   └── main.ts

4. Error messages and/or full log output:

2021/08/21 16:20:22.964 INFO    using provided configuration    {"config_file": "Caddyfile", "config_adapter": ""}
2021/08/21 16:20:22.966 INFO    redirected default logger       {"from": "stderr", "to": "stdout"}
2021/08/21 16:20:22.966 INFO    admin   admin endpoint started  {"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["localhost:2019", "[::1]:2019", "127.0.0.1:2019"]}
2021/08/21 16:20:22.966 INFO    tls.cache.maintenance   started background certificate maintenance      {"cache": "0xc0002e8af0"}
2021/08/21 16:20:22.993 INFO    pki.ca.local    root certificate is already trusted by system   {"path": "storage:pki/authorities/local/root.crt"}
2021/08/21 16:20:22.993 DEBUG   http    starting server loop    {"address": "[::]:80", "http3": false, "tls": false}
2021/08/21 16:20:22.993 INFO    tls     cleaning storage unit   {"description": "FileStorage:/home/kyz/.local/share/caddy"}
2021/08/21 16:20:22.993 INFO    autosaved config (load with --resume flag)      {"file": "/home/kyz/.config/caddy/autosave.json"}
2021/08/21 16:20:22.994 INFO    serving initial configuration
2021/08/21 16:20:22.994 INFO    tls     finished cleaning storage units

testing 1st reverse_proxy

curl localhost/index.html
404 page not found

log: empty
using firefox and brave browser, both 404 page not found
using chrome, it works the same as localhost:3000 and have a log:

2021/08/21 16:26:38.227 DEBUG   http.handlers.reverse_proxy     upstream roundtrip      {"upstream": "127.0.0.1:3000", "request": {"remote_addr": "[::1]:49606", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/favicon.ico", "headers": {"X-Forwarded-For": ["::1"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"], "Referer": ["http://localhost/"], "Accept": ["image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8"], "Accept-Encoding": ["gzip, deflate, br"], "Accept-Language": ["en-US,en;q=0.9,id;q=0.8"], "X-Forwarded-Proto": ["http"], "Sec-Ch-Ua-Mobile": ["?0"], "Sec-Fetch-Dest": ["image"], "Sec-Fetch-Site": ["same-origin"], "Sec-Fetch-Mode": ["no-cors"], "Pragma": ["no-cache"], "Cache-Control": ["no-cache"], "Sec-Ch-Ua": ["\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""]}}, "headers": {"Access-Control-Allow-Origin": ["*"], "Content-Length": ["1150"], "Content-Type": [""], "Last-Modified": ["Thu, 17 Jun 2021 04:09:06 GMT"], "Date": ["Sat, 21 Aug 2021 16:26:38 GMT"], "Keep-Alive": ["timeout=5"], "Etag": ["W/\"1150-1623902946000\""], "Cache-Control": ["no-cache"], "Connection": ["keep-alive"]}, "status": 200}

but sometimes (after killing it didn’t (have same result with firefox or brave browser), sometimes brave and chrome works (with log), but firefox still 404 without any new log on caddy

testing 1st reverse proxy with ip

curl http://127.0.0.1:80/index.html      
404 page not found

all 3 browser didn’t work, my /etc/hosts file:

cat /etc/hosts | grep localhost
127.0.0.1       localhost
::1     ip6-localhost ip6-loopback

testing static file

curl http://localhost/upload/dummy.txt
404 page not found

log: empty

not testing 2nd reverse_proxy since it’s proven but only with chrome and brave, not with firefox

5. What I already tried:

modifying /upload/* with *, with lots of combination (actually the first one is all * going to same multiple endpoints, but now i change it to /api/*, /upload/* and *),
changing port :80, without port, :7070, have no effect
already spend more than 6 hours debugging this, I don’t have such problem back then when using caddy v1

UPDATE
tried this one still the same:

{
        debug
        auto_https off
        log {
                output stdout
                level DEBUG
        }
        local_certs
}

127.0.0.1:80, localhost:80 {
        root ./svelte/dist/upload
        @notStatic {
                path /upload/*
        }
        file_server @notStatic
        reverse_proxy /api/* 127.0.0.1:9090
        reverse_proxy * 127.0.0.1:3000
}

# https://richj.co/caddy-v2-custom-404-error-pages/

Caddy sorts directives according to this predetermined directive order:

You’ll notice that reverse_proxy is ordered higher than file_server, so it will always take precedence, unless you override the order in one of multiple different ways.

I recommend using handle blocks to do this:

http://localhost {
	handle /upload/* {
		root ./svelte/dist
		file_server browse
	}

	handle /api/* {
		reverse_proxy 127.0.0.1:9090
	}

	handle {
		reverse_proxy 127.0.0.1:3000
	}
}

The handle directive allows you to define mutually exclusive request handling blocks (only the first matching handle will be run, ordered by their matchers).

2 Likes

damn…
thanks a lot β€˜__’)
v2 not as intuitive as v1, maybe because the basic example doesn’t show this feature

Caddy v1 made way more assumptions, and it was more difficult to override certain things to do exactly what you want in more complex cases.

Caddy v2 is much more flexible, but with that comes a bit more verbosity.

The fundamental rewrite of Caddy for v2 was necessary to fix tons of bugs and to make it possible to implement many more features.

1 Like

V1 had the same directive ordering thing going on, but you had way less control over things. V2 is the same in terms of easiness of configuration, but gives you more flexibility.

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