0

我知道这个问题已经被问过很多次了:

  1. Caddy - 如何仅对一个域禁用 https
  2. 禁用 caddy ssl 以启用通过 Gitlab CI 部署到 Cloud Run
  3. Caddy - 在本地域上设置 HTTPS
  4. 从 Docker 运行时如何禁用 TLS?
  5. 如何使用 Caddy 同时提供 http 和 https?

但这是我的问题。

设置

我按照他们的文档创建了一个新的 Api Platform 项目。

最简单、最强大的入门方法是下载API 平台分发版

我下载了2.5.6版本,我们可以在其中找到:

码头工人撰写

我通过删除 pwa 服务和 PostgreSQL 稍微更改了 docker compose 文件:

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

没有变化

球童档案

通过注释行进行轻微更改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
}

结果

我可以访问我的网站,https://localhost但我无法访问它,https因为球童会自动将 http流量重定向到https

问题 1

当我尝试解决方案auto_https时,它不起作用。

这是我尝试过的:

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

当我尝试访问http://localhost:80时,我被重定向到https://localhost并且我得到了This site can’t provide a secure connection

问题 2

当我尝试解决方案时:

未在配置中提供任何主机名或 IP 地址

{$SERVER_NAME}从我的 Caddyfile中删除

当我尝试访问http://localhost:80时,我被重定向到https://localhost并且我得到了This site can’t provide a secure connection

问题 3

当我尝试解决方案时:

仅在 HTTP 端口上侦听

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

当我尝试访问http://localhost:80时,我被重定向到https://localhost并且我得到了This site can’t be reached

问题

如何http在我的球童服务器上允许(并且仍然在我的球童文件中保留我的 mercure 配置)?

4

1 回答 1

0

我在这里找到了解决方案:

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

球童档案

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

码头工人撰写

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
于 2021-09-15T12:11:14.350 回答