Search code examples
gocaddycaddyfile

Caddy server tries to open port 80 instead if 8090


Here's my Caddyfile:

localhost:8090
# log log/access.log

# tls sec/cert.pem sec/key.pem

Here's I'm trying to run a Golang app:

% caddy run
2023/06/06 03:55:25.997 INFO    using adjacent Caddyfile
2023/06/06 03:55:25.999 INFO    admin   admin endpoint started  {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/06/06 03:55:26.000 INFO    tls.cache.maintenance   started background certificate maintenance      {"cache": "0xc000215c00"}
2023/06/06 03:55:26.000 INFO    http    enabling automatic HTTP->HTTPS redirects        {"server_name": "srv0"}
2023/06/06 03:55:26.001 INFO    tls     cleaning storage unit   {"description": "FileStorage:/home/me123/.local/share/caddy"}
2023/06/06 03:55:26.001 INFO    tls     finished cleaning storage units
2023/06/06 03:55:26.044 INFO    pki.ca.local    root certificate is already trusted by system   {"path": "storage:pki/authorities/local/root.crt"}
2023/06/06 03:55:26.044 INFO    http    enabling HTTP/3 listener        {"addr": ":8090"}
2023/06/06 03:55:26.044 INFO    http.log        server running  {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2023/06/06 03:55:26.044 INFO    tls.cache.maintenance   stopped background certificate maintenance      {"cache": "0xc000215c00"}
Error: loading initial config: loading new config: http app module: start: listening on :80: listen tcp :80: bind: permission denied

What does it have to do with port 80? I want to run it on 8090, why doesn't it work?


Solution

  • http    enabling automatic HTTP->HTTPS redirects
    

    Caddy implicitly activates automatic HTTPS when it knows a domain name (i.e. hostname) or IP address it is serving. One of the effects is that HTTP is redirected to HTTPS (by default, this uses HTTP port 80).

    That's why port 80 is used. And please note that it also listens on the configured port (8090 in the OP) too.

    So one of the options to prevent it from using port 80 is to prevent automatic HTTPS from being activated. Do one of these:

    • Explicitly disabling it via JSON or via Caddyfile. Here is a Caddyfile example:
      {
          auto_https off # or "disable_redirects"
      }
      
    • Not providing any hostnames or IP addresses in the config. For example, setting the address to:
      :8090
      
    • Prefixing the site address with http:// in the Caddyfile. For example:
      http://localhost:8090
      

    The other option is specifying the port to use for HTTP via Caddyfile:

    {
        http_port 801
    }
    

    http_port

    HTTPPort specifies the port to use for HTTP (as opposed to HTTPS), which is used when setting up HTTP->HTTPS redirects or ACME HTTP challenge solvers. Default: 80.

    Reference: Automatic HTTPS.