Hello,
1. The problem I’m having:
None, just wondering how to convert some Apache configuration to Caddy, perhaps in a better way.
In particular, I have the following apache.conf part that handles some rewrite rules depending on the value of the hostname being served, and doing some path rewrite and reverse proxy:
RewriteCond %{HTTP_HOST} !p1.company.net
RewriteRule . - [S=6]
RewriteRule plnext/rest/(.*)$ https://domain-1.company.net/core/$1 [P,NC]
RewriteRule core/(.*)$ https://domain-1.company.net/core/$1 [P,NC]
RewriteRule xbox/(.*)$ https://domain-2.company.net/xbox/$1 [P,NC]
RewriteRule acpui/maestro/?$ https://domain-3.company.net/acpui/maestro/index.html [P,NC]
RewriteRule acpui/maestro/(.*)$ https://domain-3.company.net/acpui/maestro/$1 [P,NC]
RewriteRule /(.*)$ https://domain-4.company.net/acpui/home/$1 [P,NC]
This section reads as:
- If host is not p1.company.net
- Skip the next 6 rules
n+1: if path match this, capture the end of the path, and reverse proxy it to a specific domain and path
I have about 10 sections like that, each containing not 6 but 19 rules.
See section on caddy file for the way I’m tackling this.
2. Error messages and/or full log output:
N/A
3. Caddy version:
2.6.4
4. How I installed and ran Caddy:
via brew (brew install caddy), and running it as “caddy run”.
a. System environment:
Mac OS, Apple M2.
b. Command:
caddy run
c. Service/unit/compose file:
N/A
d. My complete Caddy config:
:8080 {
@p1 {
host p1.company.net
}
handle @p1 {
handle_path /plnext/rest/* {
rewrite * /core/*
reverse_proxy https://domain-1.company.net {
header_up Host {upstream_hostport}
}
}
handle_path /core/* {
rewrite * /core/*
reverse_proxy https://domain-1.company.net {
header_up Host {upstream_hostport}
}
}
# Same for each and every rule
}
}
5. Links to relevant resources:
N/A
Would you see a different way?
I thought about writing a section p1.company.net:8080
instead of @p1 matcher, but this causes too much duplication (because some handlers not shown here are the exact same for all domains).