How to run multiple apps in respective path with reverse proxies?

1. Output of caddy version:

v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

2. How I run Caddy:

/usr/bin/caddy run --environ --resume --config /etc/caddy/caddyfile.json

a. System environment:

Ubuntu 22.04, systemd

b. Command:

systemctl start caddy

c. Service/unit/compose file:

# caddy.service
#
# For using Caddy with a config file.
#
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
#
# See https://caddyserver.com/docs/install for instructions.
#
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# `caddy run` command or use the caddy-api.service file instead.

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --resume --config /etc/caddy/caddyfile.json
ExecReload=/usr/bin/caddy reload --config /etc/caddy/caddyfile.json --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateDevices=yes
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddy config:

I’m so sorry, I replaced domain name because of security reasons yet

{
  "apps": {
    "http": {
      "servers": {
        "MyServer": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "@id": "domain1",
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "@id": "app_routes",
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "reverse_proxy",
                                  "transport": {
                                    "protocol": "http",
                                    "read_buffer_size": 4096
                                  },
                                  "upstreams": [
                                    {
                                      "dial": "127.0.0.1:8017"
                                    }
                                  ]
                                }
                              ],
                              "match": [
                                {
                                  "path": [
                                    "app1"
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/s/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "reverse_proxy",
                          "transport": {
                            "protocol": "http",
                            "read_buffer_size": 4096
                          },
                          "upstreams": [
                            {
                              "dial": "127.0.0.1:8080"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ],
              "match": [
                {
                  "host": [
                    "mymaskedaddress.com"
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  },
  "logging": {
    "logs": {
      "": {
        "level": "",
        "sampling": {
          "first": 0,
          "interval": 0,
          "thereafter": 0
        },
        "writer": {
          "filename": "/var/log/caddy/caddy.log",
          "output": "file",
          "roll": true,
          "roll_gzip": true,
          "roll_keep": 10,
          "roll_keep_days": 365,
          "roll_local_time": false,
          "roll_size_mb": 4
        }
      }
    }
  }
}

3. The problem I’m having:

I have multiple applications main, app1, app2 … etc. Main app is OK and it works on mymaskedaddress.com/, so I also want to make app1 and app2 to work behind reverse proxy with respective path like these: app1 on mymaskedaddress.com/s/app1 and app2 on mymaskedaddress.com/s/app2. so how can I do that within caddyfile.json?

4. Error messages and/or full log output:

I’m just getting 404 for mymaskedaddress.com/s/app1

5. What I already tried:

I tried to do that with adding subroute into caddyfile.json but I cannot make subapps work

6. Links to relevant resources:

Caddy’s official docs

Do you have any particular reason to use JSON config? If you’re getting started with learning Caddy, I strongly recommend writing a Caddyfile config first, and you can transition to a JSON config later if you have a specific reason to do so, by running the caddy adapt --pretty command.

This doesn’t look like a valid request path. This would never match, because it doesn’t start with /.

I see you nested this within another subroute with a path matcher. Path matchers don’t implicitly rewrite the URL, you would need a rewrite handler for that.

You might be looking for /s/app1* here instead.

1 Like

Thank you for your answer!
I wanted to learn the json config because of programmability, but you may be right about caddyfile, also I’m going to try /s/app1*

Thanks for answer Francis! I made it work
I have another question: is it possible to add subdomains to cloudflare DNS records from caddy?

I’m not sure I understand the question. But are you looking for GitHub - mholt/caddy-dynamicdns: Caddy app that keeps your DNS records (A/AAAA) pointed at itself. ?

1 Like

Yes, Exactly that is what I need!
Thank you very much man!

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