Search code examples
api-platform.comcaddycaddyfile

Caddy allow HTTP with Api Platform


I know this question has been asked many times:

  1. Caddy - How to disable https only for one domain
  2. Disable caddy ssl to enable a deploy to Cloud Run through Gitlab CI
  3. Caddy - Setting HTTPS on local domain
  4. How can I disable TLS when running from Docker?
  5. How to serve both http and https with Caddy?

but here is my problem.

Setup

I created a new Api Platform project following their documentation.

The easiest and most powerful way to get started is to download the API Platform distribution

I downloaded the release 2.5.6 in which we can find:

docker-compose

I slightly change the docker compose file by removing the pwa service and PostgreSQL:

version: "3.4"

services:
  php:
    build:
      context: ./api
      target: api_platform_php
    restart: unless-stopped
    env_file:
      - api/.env
    volumes:
      - php_socket:/var/run/php
    healthcheck:
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 30s

  caddy:
    build:
      context: api/
      target: api_platform_caddy
    env_file:
      - api/.env
    depends_on:
      - php
    environment:
      MERCURE_PUBLISHER_JWT_KEY: ${MERCURE_PUBLISHER_JWT_KEY:-!ChangeMe!}
      MERCURE_SUBSCRIBER_JWT_KEY: ${MERCURE_SUBSCRIBER_JWT_KEY:-!ChangeMe!}
    restart: unless-stopped
    volumes:
      - php_socket:/var/run/php
      - caddy_data:/data
      - caddy_config:/config
    ports:
      # HTTP
      - target: 80
        published: 80
        protocol: tcp
      # HTTPS
      - target: 443
        published: 443
        protocol: tcp
      # HTTP/3
      - target: 443
        published: 443
        protocol: udp

volumes:
  php_socket:
  caddy_data:
  caddy_config:

Dockerfile

No changes

Caddyfile

Slight change by commenting the line reverse_proxy @pwa http://{$PWA_UPSTREAM}

{
    # Debug
    {$DEBUG}
    # HTTP/3 support
    servers {
        protocol {
            experimental_http3
        }
    }
}

{$SERVER_NAME}

log

# Matches requests for HTML documents, for static files and for Next.js files,
# except for known API paths and paths with extensions handled by API Platform
@pwa expression `(
        {header.Accept}.matches("\\btext/html\\b")
        && !{path}.matches("(?i)(?:^/docs|^/graphql|^/bundles/|^/_profiler|^/_wdt|\\.(?:json|html$|csv$|ya?ml$|xml$))")
    )
    || {path} == "/favicon.ico"
    || {path} == "/manifest.json"
    || {path} == "/robots.txt"
    || {path}.startsWith("/_next")
    || {path}.startsWith("/sitemap")`

route {
    root * /srv/api/public
    mercure {
        # Transport to use (default to Bolt)
        transport_url {$MERCURE_TRANSPORT_URL:bolt:///data/mercure.db}
        # Publisher JWT key
        publisher_jwt {env.MERCURE_PUBLISHER_JWT_KEY} {env.MERCURE_PUBLISHER_JWT_ALG}
        # Subscriber JWT key
        subscriber_jwt {env.MERCURE_SUBSCRIBER_JWT_KEY} {env.MERCURE_SUBSCRIBER_JWT_ALG}
        # Allow anonymous subscribers (double-check that it's what you want)
        anonymous
        # Enable the subscription API (double-check that it's what you want)
        subscriptions
        # Extra directives
        {$MERCURE_EXTRA_DIRECTIVES}
    }
    vulcain
    push

    # Add links to the API docs and to the Mercure Hub if not set explicitly (e.g. the PWA)
    header ?Link `</docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation", </.well-known/mercure>; rel="mercure"`
    # Disable Google FLOC tracking if not enabled explicitly: https://plausible.io/blog/google-floc
    header ?Permissions-Policy "interest-cohort=()"

    # Comment the following line if you don't want Next.js to catch requests for HTML documents.
    # In this case, they will be handled by the PHP app.
    # reverse_proxy @pwa http://{$PWA_UPSTREAM}

    php_fastcgi unix//var/run/php/php-fpm.sock
    encode zstd gzip
    file_server
}

Result

I can acccss my website on https://localhost but I can't access it without https because caddy automatically redirect http traffic to https

Problem 1

When I try the solution auto_https it doesn't work.

Here what I tried:

{
    auto_https off
    # Debug
    {$DEBUG}
    # HTTP/3 support
    servers {
        protocol {
            experimental_http3
        }
    }
    //...
}

When I try to access http://localhost:80, I got redirect to https://localhost and I got This site can’t provide a secure connection

Problem 2

When I try the solution:

Not providing any hostnames or IP addresses in the config

I remove {$SERVER_NAME} from my Caddyfile

When I try to access http://localhost:80, I got redirect to https://localhost and I got This site can’t provide a secure connection

Problem 3

When I try the solution:

Listening exclusively on the HTTP port

services:
  # ...
  caddy:
    build:
      context: api/
      target: api_platform_caddy
    #...
    ports:
      # HTTP
      - target: 80
        published: 80
        protocol: tcp
      # HTTPS
      #- target: 443
      #  published: 443
      #  protocol: tcp
      # HTTP/3
      #- target: 443
      #  published: 443
      #  protocol: udp

When I try to access http://localhost:80, I got redirect to https://localhost and I got This site can’t be reached

Question

How can I allow http on my caddy server (and still keep my configuration with mercure in my Caddyfile) ?


Solution

  • I found a solution here:

    https://github.com/caddyserver/caddy/issues/3219#issuecomment-608236439

    Caddyfile

    {
        http_port 8080
        # Debug
        {$DEBUG}
        # HTTP/3 support
        servers {
            protocol {
                experimental_http3
            }
        }
        //...
    }
    

    docker-compose

    services:
      caddy:
        # ...
        ports:
          # HTTP
          - target: 80
            published: 80
            protocol: tcp
          - target: 8080
            published: 8080
            protocol: tcp
          # HTTPS
          - target: 443
            published: 443
            protocol: tcp
          # HTTP/3
          - target: 443
            published: 443
            protocol: udp