Need advice on how to restructure my JSON config file intended to proxy

1. The problem I’m having:

I’m using the API to push config updates. I’m sending curl -X POST commands to add routes, however my config isn’t setup properly as when I POST the new routes they end up at the end of the routes array and then the previous routes, which I was hoping would become the default behavior for all routes, don’t get applied. Clearly doing something wrong here or what I’m attempting to do isn’t optimal.

2. Error messages and/or full log output:

"routes": [
    {
        "@id": "AAAAAA-blahblahblah.com",
        "handle": [
        {
            "handler": "vars",
            "site_id": "AAAAAA"
        }],
        "match": [
        {
            "host": ["blahblahblah.com"]
        }]
    },
    {
        "@id": "BBBBBB-domain2.com",
        "handle": [
        {
            "handler": "vars",
            "site_id": "BBBBBB"
        }],
        "match": [
        {
            "host": ["domain2.com"]
        }]
    },
    {
        "@id": "default_handler",
        "handle": [
            {
                "bucket_address": "...proxysite...",
                "handler": "vars"
            },
            {
                "handler": "vars",
                "original_path": "{http.request.uri.path}"
            },
            {
                "encodings":
                {
                    "gzip":
                    {}
                },
                "handler": "encode",
                "minimum_length": 0
            },
            {
                "handler": "subroute",
                "routes": [...]
            }
        ]
    }
]

Is there a better way to organize this? I need to add multiple website routes (i.e. the blahblahblah.com and domain2.com), and then I need to apply the “@id”: “default_handler” to all of those routes in a way that isn’t dependent on route order.

I see I can PUT to …routes/0 vs POST to /routes, which more or less solves my problem but I’d appreciate any config advice if there can still be improvements.

Can you set up what you’re doing for us? Otherwise our guess is as good as yours.

Follow the help template, most of which was deleted :frowning: We are unable to help you without it.

We need to know your starting config, what commands you’re running, the result you’re getting, and the result you’re expecting, along with any errors along the way.

3. Caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

4. How I installed and ran Caddy:

System service

a. System environment:

Amazon Linux

b. Command:

This is how I’m pushing new routes to the API, which works, but I feel there has to be a better way to be applying the last route on apps/http/servers/clients/routes/. I’m really just looking for a way to apply it globally without #1 ensuring it’s always at the very end and #2 not having to copy all of that to the individual routes.

new_route = {
	    "@id": "CCCCCC-testtest2.xyz",
	    "match": [
	    	{"host": ["testtest2.xyz"]}
	    ],
	    "handle": [
	        {"handler": "vars", "site_id": "CCCCCC"}
	    ]
}


curl -X PUT "http://localhost:2019/config/apps/http/servers/clients/routes/0" -H "Content-Type: application/json" -d '${new_route}'

d. My complete Caddy config:

    "apps":
    {
        "http":
        {
            "servers":
            {
                "clients":
                {
                    "listen": [":80", ":443"],
                    "routes": [
                    {
                        "@id": "AAAAAA-testtest1.xyz",
                        "handle": [
                        {
                            "handler": "vars",
                            "site_id": "AAAAAA"
                        }],
                        "match": [
                        {
                            "host": ["testtest1.xyz"]
                        }]
                    },
                    {
                        "@id": "BBBBBB-testtest2.xyz",
                        "handle": [
                        {
                            "handler": "vars",
                            "site_id": "BBBBBB"
                        }],
                        "match": [
                        {
                            "host": ["testtest2.xyz"]
                        }]
                    },
                    {
                        "@id": "default_handler",
                        "handle": [
                        {
                            "bucket_address": "...PROXYSITE...",
                            "handler": "vars"
                        },
                        {
                            "handler": "vars",
                            "original_path": "{http.request.uri.path}"
                        },
                        {
                            "encodings":
                            {
                                "gzip":
                                {}
                            },
                            "handler": "encode",
                            "minimum_length": 0
                        },
                        {
                            "handler": "subroute",
                            "routes": [
                            {
                                "handle": [
                                {
                                    "handler": "rewrite",
                                    "uri": "/sites/{http.vars.site_id}/live/html/index.html"
                                },
                                {
                                    "handler": "reverse_proxy",
                                    "headers":
                                    {
                                        "request":
                                        {
                                            "set":
                                            {
                                                "Host": ["{http.vars.bucket_address}"]
                                            }
                                        }
                                    },
                                    "upstreams": [
                                    {
                                        "dial": "{http.vars.bucket_address}:80"
                                    }]
                                }],
                                "match": [
                                {
                                    "path": ["/"]
                                }]
                            },
                            {
                                "handle": [
                                {
                                    "body": "404 - File Not Found - /{http.vars.site_id}/live/html{http.vars.original_path}",
                                    "handler": "static_response",
                                    "status_code": 404
                                }],
                                "match": [
                                {
                                    "path": ["*.html"]
                                }]
                            },
                            {
                                "handle": [
                                {
                                    "handler": "static_response",
                                    "headers":
                                    {
                                        "Location": ["{http.request.uri}/"]
                                    },
                                    "status_code": 301
                                }],
                                "match": [
                                {
                                    "path_regexp":
                                    {
                                        "pattern": ".+[^/]$"
                                    }
                                }]
                            },
                            {
                                "handle": [
                                {
                                    "handler": "rewrite",
                                    "uri": "/sites/{http.vars.site_id}/live/html{http.vars.original_path}index.html"
                                },
                                {
                                    "handle_response": [
                                    {
                                        "match":
                                        {
                                            "status_code": [403]
                                        },
                                        "routes": [
                                        {
                                            "handle": [
                                            {
                                                "body": "404 - File Not Found - /{http.vars.site_id}/live/html{http.vars.original_path}",
                                                "handler": "static_response",
                                                "status_code": 404
                                            }]
                                        }]
                                    }],
                                    "handler": "reverse_proxy",
                                    "headers":
                                    {
                                        "request":
                                        {
                                            "set":
                                            {
                                                "Host": ["{http.vars.bucket_address}"]
                                            }
                                        }
                                    },
                                    "upstreams": [
                                    {
                                        "dial": "{http.vars.bucket_address}:80"
                                    }]
                                }]
                            }]
                        }]
                    }
                   ]
                }
            }
        }
    }
}
1 Like

That’s a pretty old version. Please update to the latest.

We added the concept of named routes in 2.7.0 which you can then use the invoke handler to trigger. Might be useful for you.

Only listen to :443, let Caddy set up HTTP->HTTPS redirects automatically. Setting :80 here breaks that.

I see now, thanks so much for elaborating.

Ok, so I think your question is: what’s the best way to make sure there’s a default/fallback/catch-all route. You don’t want to have to make sure it’s always at the end, and you want to avoid duplication.

Can I ask why having it at the end is not acceptable? I think your new method of using PUT at position 0 is correct, it is what I was going to recommend: it places the route at the beginning of the list instead of the end.

In most JSON configs, the last route, if it doesn’t have a matcher, is a “default” or “fallback” route which sounds like what you want.

So IMO the way you’re doing it now is pretty tidy.

Thank you. I updated to 2.7.5 however if I only listen to :443 I get no response at all from the server if I try to load http://, it just loads endlessly.

Ok good to know, thank you!

1 Like

Nevermind, user error! Thanks for the help.

Hey there we are actually working with Atom (Astroboy) here in Japan.

It’s nice to see home on this forum.

1 Like

Oh wow very cool, I’ve always been a fan!

1 Like

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