Unmarshalling error with a reverse proxy setup

1. Caddy version (caddy version):

v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

2. How I run Caddy:

Official docker :latest container

a. System environment:

Docker on Ubuntu 18.04

d. My complete Caddyfile or JSON config:

The config is available at JSON Editor Online - view, edit and format JSON online - my copy/paste was rejected as too long (and JSON Editor Online allows for an easier parsing on the right panel)

The pastebin version: https://pastebin.com/5C5Mz5Cb

3. The problem I’m having:

When pushing this config, I get an error message which I do not understand

4. Error messages and/or full log output:

{"level":"info","ts":1591719359.3990326,"logger":"admin.api","msg":"received request","method":"POST","host":"","uri":"/load","remote_addr":"","headers":{"Accept":["*/*"],"Accept-Encoding":["gzip, deflate"],"Connection":["keep-alive"],"Content-Length":["8846"],"Content-Type":["application/json"],"User-Agent":["python-requests/2.23.0"]}}
{"level":"info","ts":1591719359.4047804,"logger":"admin","msg":"admin endpoint started","address":"tcp/:2020","enforce_origin":false,"origins":[""]}
{"level":"warn","ts":1591719359.4048088,"logger":"admin","msg":"admin endpoint on open interface; host checking disabled","address":"tcp/:2020"}
{"level":"error","ts":1591719359.4052544,"logger":"admin.api","msg":"request error","error":"loading config: loading new config: loading http app module: decoding module config: http: json: cannot unmarshal array into Go struct field Server.servers.routes of type caddyhttp.Route","status_code":400}
{"level":"info","ts":1591719359.9052434,"logger":"admin","msg":"stopped previous server"}

I do not understand where exactly the error is, the main problem being that the error messsage does not point to a specific part of the configuration.

I see that it is in loading http app module: decoding module config: http: json: cannot unmarshal array into Go struct field Server.servers.routes of type caddyhttp.Route → so there is a weird array somewhere in the routes but I cannot see anything wrong (especially when looking at the right panel of the jsoneditoronline.com link above).

These routes come from an otherwise working setup - I just re-coded the whole thing to make it simpler to generate the routes and must have missed something.

The general idea is that caddy works as a reverse proxy. Most routes are split into LAN and non-LAN traffic (the LAN goes to the upstream server, the non-LAN returns a 200 and a space), and some are not split (in whihc case there is a match on which leads to the upstream). There are a few exceptions but that’s a general idea - I am writing this so that the setup makes more sense.

Fixed by leveraging the JSON schema error_caddy.json · GitHub.

This article might be useful for you Getting a better experience with JSON/YAML configuration

It seems like https://jsoneditoronline.org autosaves when making changes. Gross. That’s not helpful for threads like this.

@WoJ Could you post your original config elsewhere?

Scratch that, @abiosoft put the original and fixed versions in his gist.

Here’s a diff (ignoring whitespace):

diff --git a/before.txt b/after.txt
index fec82c0..55776a4 100644
--- a/before.txt
+++ b/after.txt
@@ -333,7 +333,6 @@
               "terminal": true
-            [
               "match": [
@@ -405,8 +404,7 @@
-              }
-            ],
+            },
               "match": [

I’ve pasted both configs in the gist. Also, this was how I found the error.

Sure: https://pastebin.com/5C5Mz5Cb (I will update the question too)

Because we all posted within the same minute…

See here for the diff:

Thanks but would you mind pointing to the exact issue? The JSON is correct so there must be something about its structure which does not match the expected one.

It is auto-generated so I need to understand where the issue is (as opposed to a correct JSON - thanks for that of course too)

See the diff I just posted!!! :sweat_smile:

Thanks (and @abiosoft of course for spotting the error) - I will have a close look at why my structure is incorrect.

As the diff shows you, it’s because you had a [ array where you shouldn’t have had one, on line 336.

Yes, I just tracked down the error - I forgot to enclose several matched routes (for one proxified server) with the server definition (the matching of the host to be reverse proxified, at the same level as terminal: true)

Thanks everyone!

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