Redirect domain to www subdomain

1. The problem I’m having:

I have a server with the role of reverse proxy (Caddy) and a separate server running my web services. I want all traffic from edscloud.info to redirect to www.edscloud.info in the reverse proxy. My config doesn’t seem to be working correctly (config below). I tried to follow Caddy’s documentation but it looks as if I missed something.
Also, along with corrections for my redirect I would like to know how to implement logging so in the event I do have an issue I would like to follow what is happening in the traffic through the proxy. Is it possible to implement logging in Caddy? If so, how?
Thanks.

2. Error messages and/or full log output:

Unable to find proper logs

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

a. System environment:

Installed using Caddy’s documentation on AlmaLinux 8.8 from copr repo.

b. Command:

dnf install 'dnf-command(copr)'
dnf copr enable @caddy/caddy
dnf install caddy

c. Service/unit/compose file:

systemctl status caddy
● caddy.service - Caddy
   Loaded: loaded (/usr/lib/systemd/system/caddy.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-05-22 06:37:32 EDT; 5min ago
     Docs: https://caddyserver.com/docs/
 Main PID: 3881 (caddy)
    Tasks: 7 (limit: 23225)
   Memory: 28.1M
   CGroup: /system.slice/caddy.service
           └─3881 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

d. My complete Caddy config:

edscloud.org, www.edscloud.org
{

        # Set this path to your site's directory.
        root * /usr/share/caddy

        # Enable the static file server.
        file_server

        redir https://www.edscloud.org
        # Another common task is to set up a reverse proxy:
        # Reverse to vm server teststuff
        reverse_proxy <ipaddressofwebserver>

        # Or serve a PHP site through php-fpm:
        # php_fastcgi localhost:9000

}

5. Links to relevant resources:

See the docs, there’s examples:

Yeah, that doesn’t help. I read the documentation and what I implemented from that documentation interpretation did not work. If it had worked, I would not have posted here on the forum.
What would help is telling me why my config doesn’t work and suggesting a different config change that does work and why that suggested change works.

Simply saying “it did not work” is not useful. Please explain in detail what you tried exactly, what happened when you tried it, and what you see in the logs.

Please review the help topic template, it links you to where to look to find your logs, and how to enable debug mode for more detailed logs.

1 Like

I thought my problem description was pretty clear (beside the fact that I originally entered the wrong URL). My apologies if it was not clear.
It is my understanding based on my interpretation of the documentation that Caddy reverse proxy can be configured so that if anyone were to enter a domain URL Caddy can then redirect traffic automatically to the subdomain. I attempted to configure the config file so that when anyone enters edscloud dot org, or https://edscloud.org, that traffic would automatically redirect to www dot edscloud dot org, or specifically https://www.edscloud.org, the www subdomain. I want all traffic to go to the www subdomain.
Am I incorrect in my interpretation of the Caddy documentation?
When I posted my config and described my issue I was hoping that an expert in Caddy would look at my config and tell me what I am doing wrong because with my current config, none of the domain traffic is redirecting to the www subdomain (as you can see for yourself if you attempt to go to edscloud.org).
So, again, could someone please look at my config below and tell me what I am doing wrong as I am new to Caddy and by no means a json expert.

edscloud.org, www.edscloud.org
{

        # Set this path to your site's directory.
        root * /usr/share/caddy

        # Enable the static file server.
        file_server

        # Another common task is to set up a reverse proxy:
        # Reverse to vm server
        reverse_proxy 192.168.15.16

        redir https://www.edscloud.org{uri}
        
        # Or serve a PHP site through php-fpm:
        # php_fastcgi localhost:9000
}

As for logging, here is what ‘journalctl -u caddy --no-pager | less +G’ displays with debug enabled at the time edscloud.org is accessed.

May 24 04:13:00 proxy.offworld.lan caddy[5876]: {"level":"debug","ts":1684915980.8676686,"logger":"tls.handshake","msg":"choosing certificate","identifier":"edscloud.org","num_choices":1}
May 24 04:13:00 proxy.offworld.lan caddy[5876]: {"level":"debug","ts":1684915980.8676853,"logger":"tls.handshake","msg":"default certificate selection results","identifier":"edscloud.org","subjects":["edscloud.org"],"managed":true,"issuer_key":"acme-v02.api.letsencrypt.org-directory","hash":"74304ee4dc9000cbb899d9849b3a2a27ad06eed04238b7c6316ea3b5b0d2a0de"}
May 24 04:13:00 proxy.offworld.lan caddy[5876]: {"level":"debug","ts":1684915980.8676982,"logger":"tls.handshake","msg":"matched certificate in cache","remote_ip":"154.6.82.15","remote_port":"37410","subjects":["edscloud.org"],"managed":true,"expiration":1692358590,"hash":"74304ee4dc9000cbb899d9849b3a2a27ad06eed04238b7c6316ea3b5b0d2a0de"}
May 24 04:13:00 proxy.offworld.lan caddy[5876]: {"level":"debug","ts":1684915980.9865081,"logger":"events","msg":"event","name":"tls_get_certificate","id":"be09ba44-db82-4b8a-8680-6b0345adecbc","origin":"tls","data":{"client_hello":{"CipherSuites":[4865,4867,4866,49195,49199,52393,52392,49196,49200,49162,49161,49171,49172,156,157,47,53],"ServerName":"www.edscloud.org","SupportedCurves":[29,23,24,25,256,257],"SupportedPoints":"AA==","SignatureSchemes":[1027,1283,1539,2052,2053,2054,1025,1281,1537],"SupportedProtos":["h2","http/1.1"],"SupportedVersions":[772,771],"Conn":{}}}}
May 24 04:13:00 proxy.offworld.lan caddy[5876]: {"level":"debug","ts":1684915980.9865696,"logger":"tls.handshake","msg":"choosing certificate","identifier":"www.edscloud.org","num_choices":1}
May 24 04:13:00 proxy.offworld.lan caddy[5876]: {"level":"debug","ts":1684915980.9865906,"logger":"tls.handshake","msg":"default certificate selection results","identifier":"www.edscloud.org","subjects":["www.edscloud.org"],"managed":true,"issuer_key":"acme-v02.api.letsencrypt.org-directory","hash":"c97af95983928f289e819522c7ca2a4990255994a7c8501ef29a6b1b11ee0900"}
May 24 04:13:00 proxy.offworld.lan caddy[5876]: {"level":"debug","ts":1684915980.9866054,"logger":"tls.handshake","msg":"matched certificate in cache","remote_ip":"154.6.82.15","remote_port":"37414","subjects":["www.edscloud.org"],"managed":true,"expiration":1692363287,"hash":"c97af95983928f289e819522c7ca2a4990255994a7c8501ef29a6b1b11ee0900"}

If my interpretation of the redirect documentation is incorrect, then please tell me that too.
Thank you.

You need to make separate site blocks. One is with your www subdomain which actually handles requests, and the other with the non-www subdomain which redirects to www. This is exactly what the common patterns example shows you how to do.

www.edscloud.org {
    reverse_proxy 192.168.15.16
}

edscloud.org {
    redir https://www.{host}{uri}
}
1 Like

Perfect. That seems to be working now. Thank you for the detailed example.
My little brain has trouble interpreting most documentation, especially when it comes to a coding language. :smiley:

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