Apache's ProxyPass(Reverse) for Caddy

1. The problem I’m having:

We’re transfering our webservice-logic to Caddy(v2). I need to map a remote server into the local server URL-space ( a classical /api endpoint ), I did this in Apache like this:

    <Location /api>
        ProxyPass https://api-{{ customer }}.mywebsite.com/api
        ProxyPassReverse https://api-{{ customer }}.mywebsite.com/api
        Require all granted
    </Location>

But i’m not sure how to the same in Caddy. I tried:

  handle /api* {
    reverse_proxy https://api-{{ customer }}.mywebsite.com
  }

But this does behave different than ProxyPass. Which parameter do i need to mimic the behavior from Apache?

3. Caddy version:

v2.6.4

a. System environment:

Static hostname: web-stack-test
Icon name: computer-vm
Chassis: vm
Virtualization: kvm
Operating System: Ubuntu 20.04.5 LTS
Kernel: Linux 5.4.0-144-generic
Architecture: x86-64

d. My complete Caddy config:

{
  servers {
    metrics
  }
}

api-{{ customer }}.mywebsite.com {
  metrics /metrics

  root * /var/www/api/{{ customer }}/htdocs
  file_server
  php_fastcgi 127.0.0.1:9000

  log {
    output file /var/log/caddy/api-{{ customer }}.mywebsite.com.log {
      roll_size 100mb
      roll_keep 5
      roll_keep_for 720h
    }
  }
}

{{ customer }}.mywebsite.com {
  metrics /metrics

  handle {
    root * /var/www/html
    file_server
    php_fastcgi 127.0.0.1:9001
  }

  handle /api* {
    reverse_proxy https://api-{{ customer }}.mywebsite.com
  }

  log {
    output file /var/log/caddy/{{ customer }}.mywebsite.com.log {
      roll_size 100mb
      roll_keep 5
      roll_keep_for 720h
    }
  }
}

5. Links to relevant resources:

https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass

Caddy doesn’t have any kind of “compatibility mode” to mimic any kind of specific web server behaviour of Apache.

You’d need to outline how differently it behaves, and we might be able to tell you how to achieve that behaviour in Caddy.

As far as I know they are both fairly simple reverse proxies. The basic format you’ve used for the Caddyfile configuration you tried seems correct to me, this is a very common use case for Caddy.

Thank you for your answer @Whitestrake! No problem; what I meant was simply:

{{ customer }}.mywebsite.com/api

Opening this URL when using Apache (with ProxyPass) will redirect me / all my requests to api-{{ customer }}.mywebsite.com.

But using Caddy with

  handle /api* {
    reverse_proxy https://api-{{ customer }}.mywebsite.com
  }

and then again calling

{{ customer }}.mywebsite.com/api

does nothing beside showing me the main website on {{ customer }}.mywebsite.com, when it should redirect my requests to api-{{ customer }}.mywebsite.com because of the handle handle /api*. Is there a simple misconfiguration in my config?

I don’t think your Caddyfile is misconfigured, per se. But from the issue you’ve described, it’s possible you’re not running the configuration you think you are, or maybe you’re not making the requests you think you are.

To demonstrate:

whitestrake in ~ at merlin
➜ caddy version
v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

whitestrake in ~ at merlin
➜ cat Caddyfile
localhost {
  handle {
    respond "I'm the webroot"
  }

  handle /api* {
    respond "I'm the API"
  }
}

whitestrake in ~ at merlin
➜ curl -L localhost
I'm the webroot⏎

whitestrake in ~ at merlin
➜ curl -L localhost/api
I'm the API⏎

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