1. Output of caddy version
:
v2.5.2 h1:eCJdLyEyAGzuQTa5Mh3gETnYWDClo1LjtQm2q9RNZrs=
2. How I run Caddy:
Runs as a service, primary config is /etc/caddy/Caddyfile which contains
import /etc/caddy/redirects/*.cnf
The purpose of this server is redirects and all of the files in the redirects directory are created by a script and are in this form:
fromDomain.tld www.fromDomain.tld {
redir https://toDomain.tld{uri} permanent
}
We have no issues with this setup, it is an exception to this setup I am writing about.
a. System environment:
OS: AlmaLinux release 8.6 (Sky Tiger)
Kernel: 4.18.0-348.23.1.el8_5.x86_64
Headless server instance running in a VPC on AWS - EC2 instance t3a.medium
systemd 239 (239-58.el8_6.3)
No Docker, no desktop, no anything else really except for some custom created scripts for managing Route 53 zones and the redirects.
b. Command:
I don’t type anything to run it, it is a service. Service status is as follows
Alias tip: S status caddy
● caddy.service - Caddy
Loaded: loaded (/usr/lib/systemd/system/caddy.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2022-08-09 06:16:45 PDT; 6 days ago
Docs: https://caddyserver.com/docs/
Main PID: 899223 (caddy)
Tasks: 12 (limit: 12008)
Memory: 57.2M
CGroup: /system.slice/caddy.service
└─899223 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
If I add or remove a redirect my script either creates a new config file from a template or deletes the config file and then restarts the caddy service.
c. Service/unit/compose file:
N/A
d. My complete Caddy config:
As above
import /etc/caddy/redirects/*.cnf
and a sample config, because I am not going to post over 350 almost identical files
fromDomain.tld www.fromDomain.tld {
redir https://toDomain.tld{uri} permanent
3. The problem I’m having:
I have a need to have one of the redirects behave differently, but only if the remote IP is a certain IP. I want it to act normally otherwise. I have read the documentation but I am still unsure how to go about this. So for this particular config file I have appended a line I got from (Request matchers (Caddyfile) — Caddy Documentation) to the top of the file, but then I don’t know what to do with it, and how to have any other IPs that doesn’t match just do the redirect.
Here is as far as I got before I got lost.
@pinger remote_ip 203.0.113.14
fromDomain.tld www.fromDomain.tld {
redir https://toDomain.tld{uri} permanent
Once I have defined @pinger, then what do I do? What does the config file then look like. For example, if I wanted that one IP to serve a static web page on the file system, and all other IPs to just do the redirect, how would that look?
4. Error messages and/or full log output:
N/A
5. What I already tried:
Not much. I have read the following:
I’m new and can’t post links - /docs/caddyfile/matchers#remote-ip
I’m new and can’t post links - /docs/modules/http.matchers.remote_ip
I’m new and can’t post links - /docs/caddyfile/patterns
I’m new and can’t post links - /docs/caddyfile/directives
and I have no better idea how to proceed than I did before I read those pages.