'Caddy reverse_proxy with websocket JSON config

I have this simple caddy JSON config to proxy request from https://localhost to my localhost server running on port 8080. That's working fine.

{
    "apps": {
        "http": {
            "servers": {
                "localhost": {
                    "listen": [":443"],
                    "routes": [
                        {
                            "match": [
                                {
                                    "host": ["localhost"]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "reverse_proxy",
                                    "upstreams": [
                                        {
                                            "dial": "localhost:8080"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
}

But, I would also like my caddy server to either:

  • reverse proxy calls from wss:localhost:3000/ws to ws:localhost:3000/ws (HTTPS to HTTP)
  • or simply ignore websocket calls and let them reach ws:localhost:3000/ws

I have not found the proper example or documentation for JSON config.



Solution 1:[1]

Check if Caddy 2.5 (Apr. 2022) could help in your use case.

It comes with Reverse proxy: Dynamic upstreams:

The ability to get the list of upstreams at every request (more specifically, every iteration in the proxy loop of every request) rather than just once at config-load time.

Dynamic upstream modules can be plugged in to provide Caddy with the latest list of backends in real-time.
Two standard modules have been implemented which can get upstreams from SRV and A/AAAA record lookups.

Warning: This deprecates the lookup_srv JSON field for upstreams (and srv+ scheme prefix in the Caddyfile), which will be removed in the future.

See PR 4470:

Right now, proxy upstreams have to be hard-coded into the config (with the only dyanamism coming from placeholders, which all act as a single upstream anyway).

This change adds supports for truly dynamic upstreams, with the potential for every request to have different upstreams -- not only every request, but every retry within a single request, too.

Instead of (or in addition to) specifying upstreams in your config, you could specify dynamic_upstreams and then define your upstream source module. Currently I'm implementing SRV and A/AAAA lookups as sources.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC