Dies ist eher ein Proof of Concept als ein voll funktionsfähiges Projekt. Dies versucht, die Caddy JSON -API -Struktur zu replizieren, um durch kettenfähige PHP -Klassen zu arbeiten.
Im Moment gibt es nur eine winzige Untergruppe von Befehlen von Caddy 2.0, die meinen derzeit benötigten Anwendungsfall behandelt.
composer require mattvb91/caddy-phpEin grundlegendes Beispiel für einen HTTP -Server mit einer statischen Antwort:
$ caddy = new Caddy ();
$ caddy -> addApp (
( new Http ())-> addServer (
' server1 ' , ( new Http Server ())-> addRoute (
( new Route ())-> addHandle (
new StaticResponse ( ' Hello world ' , 200 )
)
))
);
$ caddy -> load ();Dies führt zu der folgenden CADDY -Konfiguration:
{
"admin" : {
"disabled" : false ,
"listen" : " :2019 "
},
"apps" : {
"http" : {
"servers" : {
"server1" : {
"listen" : [
" :80 "
],
"routes" : [
{
"handle" : [
{
"handler" : " static_response " ,
"body" : " Hello world " ,
"status_code" : 200
}
]
}
]
}
}
}
}
}curl -v localhost
-----
< HTTP/1.1 200 OK
< Server: Caddy
Hello world Wenn Sie Hostnamen dynamisch verwalten (in einer Datenbank) und die Konfiguration nicht mit einer Liste vorhandener Hostnamen erstellen können, da Sie sie zur Laufzeit verwalten müssen, können Sie Folgendes ausführen:
Der wichtige Teil in diesem Beispiel ist die Kennung host_group_name , mit der später Domänen zu diesem Host hinzugefügt / entfernen wird.
$ caddy = new Caddy ();
$ caddy -> addApp (
( new Http ())-> addServer (
' server1 ' , ( new Http Server ())-> addRoute (
( new Route ())-> addHandle (
new StaticResponse ( ' host test ' , 200 )
)-> addMatch (( new Host ( ' host_group_name ' ))
-> setHosts ([ ' localhost ' ])
)
)-> addRoute (( new Route ())
-> addHandle ( new StaticResponse ( ' Not found ' , 404 ))
-> addMatch (( new Host ( ' notFound ' ))
-> setHosts ([ ' *.localhost ' ])
)
))
);
$ caddy -> load ();Später in einem Skript oder Ereignis in Ihrem System können Sie Ihr Caddy -Konfigurationsobjekt und eine neue Domäne unter dieser Route veröffentlichen:
$ caddy -> addHostname ( ' host_group_name ' , ' new.localhost ' )
$ caddy -> addHostname ( ' host_group_name ' , ' another.localhost ' )curl -v new.localhost
> GET / HTTP/1.1
> Host: new.localhost
>
< HTTP/1.1 200 OK
curl -v another.localhost
> GET / HTTP/1.1
> Host: another.localhost
>
< HTTP/1.1 200 OK $ caddy -> syncHosts ( ' host_group_name ' ); //Sync from caddy current hostname list
$ caddy -> removeHostname ( ' host_group_name ' , ' new.localhost ' );
$ caddy -> removeHostname ( ' host_group_name ' , ' another.localhost ' );curl -v new.localhost
> GET / HTTP/1.1
> Host: new.localhost
>
< HTTP/1.1 404 Not Found
curl -v another.localhost
> GET / HTTP/1.1
> Host: another.localhost
>
< HTTP/1.1 404 Not Found Nehmen wir einen Fall, in dem Sie einen Knotenfrontend und einen PHP -Backend -Anfragen auf der Route /api/* erhalten möchten. In diesem Fall bricht das Beispiel auf 2 umgekehrte Proxy mit einem Routenpaar, um die /api/* in den PHP stromaufwärts zu filtern.
Dies setzt voraus, dass die 3 Hosts (Caddy, Knoten, PHP) alle Docker -Container sind und mit dem Containernamen im selben Docker -Netzwerk zugänglich sind. Möglicherweise müssen Sie Ihre Hostnamen nach Bedarf anpassen.
use mattvb91 CaddyPhp Caddy ;
use mattvb91 CaddyPhp Config Apps Http ;
use mattvb91 CaddyPhp Config Apps Http Server ;
use mattvb91 CaddyPhp Config Apps Http Server Route ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Handle ReverseProxy ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Handle ReverseProxy Transport FastCGI ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Handle ReverseProxy Upstream ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Handle Subroute ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Match Host ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Match Path ;
$ apiReverseProxy = ( new ReverseProxy ())
-> addUpstream (( new Upstream ())
-> setDial ( ' laravel-api:9000 ' )
)-> addTransport (( new FastCGI ())
-> setRoot ( ' /app/public/index.php ' )
-> setSplitPath ([ '' ])
);
$ apiMatchPath = ( new Path ())
-> setPaths ([
' /api/* ' ,
]);
$ backendAPIRoute = ( new Route ())
-> addHandle ( $ apiReverseProxy )
-> addMatch ( $ apiMatchPath );
$ route = new Route ();
$ route -> addHandle (( new Subroute ())
-> addRoute ( $ backendAPIRoute )
-> addRoute (( new Route ())
-> addHandle (( new ReverseProxy ())
-> addUpstream (( new Upstream ())
-> setDial ( ' nextjs:3000 ' )
)
)
)
)-> addMatch (( new Host ())
-> setHosts ([
' localhost ' ,
])
)-> setTerminal ( true );
$ caddy = new Caddy ();
$ caddy -> addApp (( new Http ())
-> addServer ( ' myplatform ' , ( new Server ())
-> addRoute ( $ route )
)
);
$ caddy -> load ();Dadurch wird die folgende CADDY -Konfiguration veröffentlicht:
{
"admin" : {
"disabled" : false ,
"listen" : " :2019 "
},
"apps" : {
"http" : {
"servers" : {
"myplatform" : {
"listen" : [
" :80 "
],
"routes" : [
{
"handle" : [
{
"handler" : " subroute " ,
"routes" : [
{
"handle" : [
{
"handler" : " reverse_proxy " ,
"transport" : {
"protocol" : " fastcgi " ,
"root" : " /app/public/index.php " ,
"split_path" : [
" "
]
},
"upstreams" : [
{
"dial" : " laravel-api:9000 "
}
]
}
],
"match" : [
{
"path" : [
" /api/* "
]
}
]
},
{
"handle" : [
{
"handler" : " reverse_proxy " ,
"upstreams" : [
{
"dial" : " nextjs:3000 "
}
]
}
]
}
]
}
],
"match" : [
{
"host" : [
" localhost "
]
}
],
"terminal" : true
}
]
}
}
}
}
}curl -v localhost
< HTTP/1.1 200 OK
< Content-Type: text/html ; charset=utf-8
< Server: Caddy
< X-Powered-By: Next.js
< Transfer-Encoding: chunked
<
< ! DOCTYPE html><html > ....curl -v localhost/api/testroute
< HTTP/1.1 200 OK
< Content-Type: application/json
< Server: Caddy
< X-Powered-By: PHP/8.1.7
<
{ " status " :200}
Schauen Sie sich die Tests an, um weitere Beispiele zu erhalten.