Could not find documentation for using redir in caddy json file

1. Caddy version (caddy version):

v2.3

2. How I run Caddy:

caddy run --config=caddy.json

a. System environment:

Ubuntu 20.04

b. Command:

Paste command here.

caddy run --config=caddy.json

c. Service/unit/compose file:

Paste full file contents here.
Make sure backticks stay on their own lines,
and the post looks nice in the preview pane.

d. My complete Caddyfile or JSON config:

{
        "apps": {
                "http": {
                        "servers": {
                                "example": {
                                        "listen": [":80",":443"],
                                        "routes": [
                                                {
                                                        "match": [{"host": ["example.com"]}],
                                                        "handle": [
                                                                {
                                                                "handler": "reverse_proxy",
                                                                "upstreams": [
                                                                                {"dial":"xxx.xxx.xxx.xxx:5000"}]
                                                        }]
                                                }
                                        ]
                                }
                        }
                }
        }
}
Paste config here, replacing this text.
Use `caddy fmt` to make it readable.
DO NOT REDACT anything except credentials.
LEAVE DOMAIN NAMES INTACT.
Make sure the backticks stay on their own lines.

3. The problem I’m having:

I want to redirect http://example.com to https://example.com. Because it is not working by default.
So for achieving this, i was thinking of using redir directive but didn’t find any example of using redir in json format file.
Please assist where should I put redir directive to redirect http://example.comhttps://example.com

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

Just remove :80 from here and it will work.

Caddy sets up Automatic HTTPS if your config has a host matcher with a valid domain, which enables port 80 implicitly for the HTTP to HTTPS redirects and to solve ACME HTTP challenges.

When learning how to write JSON configs, I strongly recommend writing what you want with the Caddyfile first, then running caddy adapt --pretty to see the underlying JSON config.

But anyways, if you do ever need to do a redirect manually, you do it with the static_response handler plus the setting the Location header on that response (which is how redirects are performed)

2 Likes

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