ReverseProxy and Subdirectories

1. Caddy version (v2):

2. How I run Caddy:

a. System environment:

Ubuntu 20.04 LTS, Package Installed Caddy

b. Command:

systemctl (start,stop,restart,status) caddy.service

d. My complete Caddyfile or JSON config:


https://home.example.com {

    reverse_proxy * 192.168.125.11:443 {
        header_up X-Forwarded-Proto https
        header_up Host home.example.com
        transport http {
            tls_insecure_skip_verify

        }

    }
}

https://cloud.example.com {

    reverse_proxy * 192.168.125.12:443 {
        rewrite /nextcloud /
        header_up Host cloud.example.com
        header_up X-Forwarded-Proto https
        transport http {
            tls_insecure_skip_verify
        }

    }
}

https://cad.example.com {
    rewrite /Cad /
    reverse_proxy * 192.168.125.13:80 {
        header_up Host bauad.example.com
        header_up X-Forwarded-Proto https
    }
}

3. The problem I’m having:

I have the simple problem that I would have access to the subfolders of the respective web server with the reverse proxy.

(1) The first functions as far as it has no subfolder.
proxy: https:// home.example. com direct: https:// 192.168.125.11

(2) The second:
proxy: https:// cloud.example. com direct: https:// 192.168.125.12/nextcloud

(3) The Third:
proxy: https:// cad.example. com direct: http:// 192.168.125.13/cad

I do not care whether I go directly to the respective folder or whether it is forwarded.

2: https://cloud.example.com/nextcloud
3: https://cad.example.com/cad

are also solutions that are considered possible.

5. What I already tried:

as seen in the caddyfile above, I have already tried “rewrite”. Even with “redir” it didn’t work.

This seems to be a simple configuration but I haven’t found a working solution.

I just had to configure something similar (my path on the upstream is a bit more complicated).

at the moment i use this:

www.example.com {
        tls mail@example.com {
                #ca https://acme-staging-v02.api.letsencrypt.org/directory
        }
        route  {
                uri replace / /some/path/on/upstream/ 1
                reverse_proxy http://upstream.example.com {
                    import someheaders
                }
        }
}

the final “1” on the uri line is importatnt to get things working, without it the “/” got replaced multiple times :grimacing:

To prepend to the path, use rewrite instead:

rewrite * /some/path/on/upstream{uri}

@Northrop I wrote a very comprehensive wiki article about composing routes in the Caddyfile – it will answer your question! (As will the docs page for the route directive.)

1 Like

ok, that is more like my former apache config, were i used a RewriteRule with [L,P] to proxy it to the upstream.
It works :grinning: (even without explicit route{} )

1 Like

To me, rewriting the URI seems the easiest. Because I don’t quite understand the matt documentation. I cannot draw a solution to my problem from the documentation.

The change with rewrite solves the problem for me: ERR_TOO_MANY_REDIRECTS

https://cloud.example.com {

    route {
      rewrite * /nextcloud{uri}
      reverse_proxy * 192.168.125.12:443 {
          header_up Host cloud.example.com
          header_up X-Forwarded-Proto https
          transport http {
              tls_insecure_skip_verify
          }
      }
    }
}

Ok, This was my mistake. i found the error. For the rewrite, the * had to be replaced by /

https://cloud.example.com {

    route {
      rewrite / /nextcloud{uri}
      reverse_proxy * 192.168.125.12:443 {
          header_up Host cloud.example.com
          header_up X-Forwarded-Proto https
          transport http {
              tls_insecure_skip_verify
          }
      }
    }
}
1 Like

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