JSON config for reverse proxying to a path/subdirectory

1. Caddy version (caddy version):

2.4

2. How I run Caddy:

Caddy API as a systemd service

a. System environment:

Ubuntu 20.04

b. Command:

service caddy-api start

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

{
   "admin": {
      "disabled": false,
      "listen": "localhost:2019"
   },
   "apps": {
      "http": {
         "servers": {
            "srv0": {
               "listen": [
                  ":80",
                  ":443"
               ],
               "routes": [
                  {
                     "handle": [
                        {
                           "encodings": {
                              "gzip": {}
                           },
                           "handler": "encode"
                        },
                        {
                           "handler": "reverse_proxy",
                           "headers": {
                              "request": {
                                 "set": {
                                    "Host": [
                                       "somedomain.com"
                                    ],
                                    "X-Forwarded-Proto": [
                                       "https"
                                    ],
                                    "apx-incoming-host": [
                                       "somedomain.com"
                                    ]
                                 }
                              },
                              "response": {
                                 "set": {
                                    "apx-hit": [
                                       "true"
                                    ]
                                 }
                              }
                           },
                           "transport": {
                              "protocol": "http",
                              "tls": {}
                           },
                           "upstreams": [
                              {
                                 "dial": "targetdomain.com:443"
                              }
                           ]
                        }
                     ],
                     "match": [
                        {
                           "host": [
                              "somedomain.com"
                           ]
                        }
                     ],
                     "terminal": true,
                     "@id": "vhost_1012"
                  }
               ]
            }
         }
      },
      "tls": {
         "automation": {
            "on_demand": {
               "ask": "https://mydomain.com/check-domain"
            }
         }
      }
   },
   "logging": {
      "logs": {
         "default": {
            "level": "DEBUG"
         }
      }
   }
}

3. The problem I’m having:

I’m wondering if there’s a way to reverse proxy somedomain.com to targetdomain.com/some-subdirectory in a way that would append any additional paths to the end like this somedomain.com/another-path reverse proxies to targetdomain.com/some-subdirectory/another-path.

To the user, it should just look like they’ve gone to somedomain.com but they get the same results as if they went to targetdomain.com/some-subdirectory.

4. Error messages and/or full log output:

No error log on this one

5. What I already tried:

Searching the docs, but I haven’t found anything really related to this specifically. They’re also taking 1+ minutes to load a page which has made the search slow enough that I decided to ask on here instead.

6. Links to relevant resources:

N/A

Yes, use the rewrite handler before reverse_proxy, and using a target "uri" of /subdir{http.request.uri}.

Docs for the rewrite handler (yes, sorry that it takes so long to load :frowning_face:):

In general, I recommend trying to do what you want with the Caddyfile first, then running caddy adapt --pretty to see how the JSON looks.

2 Likes

Thank you Francis! Much appreciated.

1 Like

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