Caddy允许HTTP与Api平台



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

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

但这就是我的问题。

<标题>

设置我根据他们的文档创建了一个新的Api平台项目。

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

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

  • docker-compose
  • Dockerfile
  • Caddyfile
  • 和许多其他文件

docker-compose

我通过删除pwa服务和PostgreSQL稍微改变了docker组合文件:

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

没有变化

Caddyfile

通过注释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,因为caddy自动重定向http流量到https

<标题>

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

这是我尝试的:

  • 添加auto_https disable_redirectsauto_https off
{
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 h1> 我尝试解决方案时:

未在配置中提供任何主机名或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在我的caddy服务器(仍然保持我的配置与mercure在我的caddy文件)?

使用api平台模板v3

docker-compose.yml

services:
caddy:
environment:
# SERVER_NAME: ${SERVER_NAME:-localhost}, caddy:80
# allow a page with http on different port.
SERVER_NAME: localhost:443, http://localhost:8080/page.php, caddy:80
# allow http completely on port 80.
SERVER_NAME: localhost:443, localhost:80
ports:
# HTTP
- target: 80
published: ${HTTP_PORT:-80}
protocol: tcp
# HTTP Legacy
- target: 8080
published: 8080
protocol: tcp
# HTTPS
- target: 443
published: ${HTTPS_PORT:-443}
protocol: tcp
# HTTP/3
- target: 443
published: ${HTTP3_PORT:-443}
protocol: udp

我找到了一个解决方案:

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

最新更新