Caddy rewrite request redirects to reverse_proxy upstream

1. Caddy version (caddy version):

2. How I run Caddy:

v2.2.2 h1:Ha3bvEvkb/GLGEX648/qI5zTt6uJCnfQhZHmZBxhzDY=

a. System environment:

systemd

b. Command:

sudo systemctl start  caddy

d. My complete Caddyfile or JSON config:

{
  "admin": { "listen": ":2020" },
  "storage": {
    "module": "s3",
    "host": "#######",
    "bucket": "staging",
    "access_key": "#######",
    "secret_key": "#######",
    "prefix": "ssl",
    "redis_address": "127.0.0.1:6379",
    "redis_password": "",
    "redis_db": 0
  },
  "logging": {
    "logs": {
      "default": { "exclude": ["http.log.access.log0"] },
      "log0": {
        "writer": { "filename": "/var/lib/caddy/log/access.log", "output": "file" },
        "include": ["http.log.access.log0"]
      }
    }
  },
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [":443"],
          "routes": [
             {
              "handle": [
                {
                  "handler": "rewrite",
                  "uri_substring": [
                    {
                      "find": "{http.request.uri}",
                      "replace": "/image/v2/thumbnail/{http.request.uri}"
                    }
                  ]
                },
                {
                  "handler": "reverse_proxy",
                  "headers": {
                    "request": {
                      "set": { "Host": ["two.domaintwo.com"] }
                    },
                    "response": {
                      "set": { "Host": ["{http.request.host}"] }
                    }
                  },
                  "transport": { "protocol": "http", "tls": {} },
                  "upstreams": [{ "dial": "two.domaintwo.com" }]
                }
              ]
            }
          ],
          "logs": { "default_logger_name": "log0" }
        }
      }
    },
    "tls": {
      "automation": {
        "policies": [
          {
            "issuer": { "email": "two@gmail.com", "module": "acme" },
            "on_demand": true
          }
        ],
        "on_demand": {
          "ask": "https://two.domain2.com/api/v1/whitelabel/validate"
        }
      }
    }
  }
}

3. The problem I’m having:

What I’m trying to achive is domain white labelling for customers.In the given situation
if a user makes request on one.domainone.com/id123 the request should be served from two.domaintwo.com/image/v2/thumbnail/id123. My current configuration is redirecting me to
two.domaintwo.com/image/v2/thumbnail/id123 when I try to visit one.domainone.com/id123

4. Error messages and/or full log output:

5. What I already tried:

I tried going through Caddyserver documentation and setup the whole config while, I am kinda stuck with this rewrite situation. proxy setup is little bit new for me

6. Links to relevant resources:

I see you’re using the broken 2.2.2 release; please run sudo apt install caddy=2.2.1. See the below post for the reason:

For your rewrite, it’s easier to write in Caddyfile, then use caddy adapt to see what the underlying JSON looks like. What you’re looking for is this:

rewrite * /image/v2/thumbnail{uri}

You don’t need to do a find and replace, you can just rewrite to add the suffix and append the existing uri.

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