Import redirects from external URL

1. The problem I’m having:

I’m trying to import an external file of redir declarations, but Caddy says the file is not found. I think I have my Caddyfile syntax wrong:

https://appliedselfdirection.com {
        encode gzip zstd

        root * /var/www/asd/appliedselfdirection.com/master

        file_server

        import https://appliedselfdirection.com/redirects

        handle_errors {
                @404 {
                        expression {http.error.status_code} == 404
                }
                rewrite @404 /404.html
                file_server
        }

        log {
                output file /var/lib/caddy/web_access.log
        }
}

2. Error messages and/or full log output:

Feb 12 04:15:56 caddy caddy[289143]: Error: adapting config using caddyfile: /var/lib/caddy/Caddyfile:34 - Error during parsing: File to import not found: https://appliedselfdirection.com/redirects

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

I use the binary directly in a systemd service.

a. System environment:

Ubuntu 22.10 (systemd)

b. Command:

/var/lib/caddy/caddy run --environ --config /var/lib/caddy/Caddyfile

c. Service/unit/compose file:

Contents of /etc/systemd/system/caddy.service:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/var/lib/caddy/caddy run --environ --config /var/lib/caddy/Caddyfile
ExecReload=/var/lib/caddy/caddy reload --config /var/lib/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateDevices=yes
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddy config:

https://www.appliedselfdirection.com {
        redir https://appliedselfdirection.com{uri}
}
https://appliedselfdirection.com {
        encode gzip zstd

        root * /var/www/asd/appliedselfdirection.com/master

        file_server

        import https://appliedselfdirection.com/redirects

        handle_errors {
                @404 {
                        expression {http.error.status_code} == 404
                }
                rewrite @404 /404.html
                file_server
        }

        log {
                output file /var/lib/caddy/web_access.log
        }
}

5. Links to relevant resources:

Similar issue: Read redirects from file

You can only import files from files on disk.

Importing config from the internet would be an exceptionally terrible idea.

Thanks for the quick response @francislavoie! Just so I understand the risk, if the endpoint is a service you control, is the concern that this gets tampered with during transit?

The setup above was only supposed to be temporary, as it is currently I could actually reference the redirects file from the local disk on the server. Ultimately though, I wanted to use Caddy as a static file webserver, but have the ability for our database application (PocketBase on a separate server) to provide an endpoint that produces the redirect list dynamically. Thanks!

You could write a script (and configure it in your crontab or whatever) to download the list of redirects, update the imported file, then reload Caddy.

But Caddy will not download config files. That would be bad.

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