fix: reload caddy issue

This commit is contained in:
Andras Bacsai 2024-03-12 10:42:56 +01:00
parent 6950966b06
commit b576014d07
4 changed files with 113 additions and 90 deletions

View File

@ -54,8 +54,7 @@ class Proxy extends Component
SaveConfiguration::run($this->server, $this->proxy_settings); SaveConfiguration::run($this->server, $this->proxy_settings);
$this->server->proxy->redirect_url = $this->redirect_url; $this->server->proxy->redirect_url = $this->redirect_url;
$this->server->save(); $this->server->save();
$this->server->setupDefault404Redirect();
setup_default_redirect_404(redirect_url: $this->server->proxy->redirect_url, server: $this->server);
$this->dispatch('success', 'Proxy configuration saved.'); $this->dispatch('success', 'Proxy configuration saved.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
@ -66,6 +65,9 @@ class Proxy extends Component
{ {
try { try {
$this->proxy_settings = CheckConfiguration::run($this->server, true); $this->proxy_settings = CheckConfiguration::run($this->server, true);
SaveConfiguration::run($this->server, $this->proxy_settings);
$this->server->save();
$this->dispatch('success', 'Proxy configuration saved.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} }

View File

@ -120,6 +120,106 @@ class Server extends BaseModel
} }
} }
} }
public function setupDefault404Redirect()
{
$dynamic_conf_path = $this->proxyPath() . "/dynamic";
$proxy_type = $this->proxyType();
$redirect_url = $this->proxy->redirect_url;
if ($proxy_type === 'TRAEFIK_V2') {
$default_redirect_file = "$dynamic_conf_path/default_redirect_404.yaml";
} else if ($proxy_type === 'CADDY') {
$default_redirect_file = "$dynamic_conf_path/default_redirect_404.caddy";
}
if (empty($redirect_url)) {
instant_remote_process([
"mkdir -p $dynamic_conf_path",
"rm -f $default_redirect_file",
], $this);
return;
}
if ($proxy_type === 'TRAEFIK_V2') {
$dynamic_conf = [
'http' =>
[
'routers' =>
[
'catchall' =>
[
'entryPoints' => [
0 => 'http',
1 => 'https',
],
'service' => 'noop',
'rule' => "HostRegexp(`{catchall:.*}`)",
'priority' => 1,
'middlewares' => [
0 => 'redirect-regexp@file',
],
],
],
'services' =>
[
'noop' =>
[
'loadBalancer' =>
[
'servers' =>
[
0 =>
[
'url' => '',
],
],
],
],
],
'middlewares' =>
[
'redirect-regexp' =>
[
'redirectRegex' =>
[
'regex' => '(.*)',
'replacement' => $redirect_url,
'permanent' => false,
],
],
],
],
];
$conf = Yaml::dump($dynamic_conf, 12, 2);
$conf =
"# This file is automatically generated by Coolify.\n" .
"# Do not edit it manually (only if you know what are you doing).\n\n" .
$conf;
$base64 = base64_encode($conf);
} else if ($proxy_type === 'CADDY') {
$conf = ":80, :443 {
redir $redirect_url
}";
ray($conf);
$conf =
"# This file is automatically generated by Coolify.\n" .
"# Do not edit it manually (only if you know what are you doing).\n\n" .
$conf;
$base64 = base64_encode($conf);
}
instant_remote_process([
"mkdir -p $dynamic_conf_path",
"echo '$base64' | base64 -d > $default_redirect_file",
], $this);
if (config('app.env') == 'local') {
ray($conf);
}
if ($proxy_type === 'CADDY') {
$this->reloadCaddy();
}
}
public function setupDynamicProxyConfiguration() public function setupDynamicProxyConfiguration()
{ {
$settings = InstanceSettings::get(); $settings = InstanceSettings::get();
@ -250,8 +350,7 @@ class Server extends BaseModel
instant_remote_process([ instant_remote_process([
"rm -f $file", "rm -f $file",
], $this); ], $this);
// $this->reloadCaddy(); $this->reloadCaddy();
} else { } else {
$url = Url::fromString($settings->fqdn); $url = Url::fromString($settings->fqdn);
$host = $url->getHost(); $host = $url->getHost();
@ -264,14 +363,15 @@ $schema://$host {
instant_remote_process([ instant_remote_process([
"echo '$base64' | base64 -d > $file", "echo '$base64' | base64 -d > $file",
], $this); ], $this);
// $this->reloadCaddy(); $this->reloadCaddy();
} }
} }
} }
} }
public function reloadCaddy() { public function reloadCaddy()
{
return instant_remote_process([ return instant_remote_process([
"docker exec coolify-proxy caddy reload --config /dynamic/Caddyfile", "docker exec coolify-proxy caddy reload --config /config/caddy/Caddyfile.autosave",
], $this); ], $this);
} }
public function proxyPath() public function proxyPath()

View File

@ -218,80 +218,3 @@ function generate_default_proxy_configuration(Server $server)
SaveConfiguration::run($server, $config); SaveConfiguration::run($server, $config);
return $config; return $config;
} }
function setup_default_redirect_404(string|null $redirect_url, Server $server)
{
$traefik_dynamic_conf_path = $server->proxyPath() . "/dynamic";
$traefik_default_redirect_file = "$traefik_dynamic_conf_path/default_redirect_404.yaml";
if (empty($redirect_url)) {
instant_remote_process([
"mkdir -p $traefik_dynamic_conf_path",
"rm -f $traefik_default_redirect_file",
], $server);
} else {
$traefik_dynamic_conf = [
'http' =>
[
'routers' =>
[
'catchall' =>
[
'entryPoints' => [
0 => 'http',
1 => 'https',
],
'service' => 'noop',
'rule' => "HostRegexp(`{catchall:.*}`)",
'priority' => 1,
'middlewares' => [
0 => 'redirect-regexp@file',
],
],
],
'services' =>
[
'noop' =>
[
'loadBalancer' =>
[
'servers' =>
[
0 =>
[
'url' => '',
],
],
],
],
],
'middlewares' =>
[
'redirect-regexp' =>
[
'redirectRegex' =>
[
'regex' => '(.*)',
'replacement' => $redirect_url,
'permanent' => false,
],
],
],
],
];
$yaml = Yaml::dump($traefik_dynamic_conf, 12, 2);
$yaml =
"# This file is automatically generated by Coolify.\n" .
"# Do not edit it manually (only if you know what are you doing).\n\n" .
$yaml;
$base64 = base64_encode($yaml);
instant_remote_process([
"mkdir -p $traefik_dynamic_conf_path",
"echo '$base64' | base64 -d > $traefik_default_redirect_file",
], $server);
if (config('app.env') == 'local') {
ray($yaml);
}
}
}

View File

@ -13,7 +13,8 @@
<x-forms.button type="submit">Save</x-forms.button> <x-forms.button type="submit">Save</x-forms.button>
</div> </div>
<div class="pb-4 ">Before switching proxies, please read <a href="https://coolify.io/docs/server/switching-proxies">this</a>.</div> <div class="pb-4 ">Before switching proxies, please read <a
href="https://coolify.io/docs/server/switching-proxies">this</a>.</div>
@if ($server->proxyType() === 'TRAEFIK_V2') @if ($server->proxyType() === 'TRAEFIK_V2')
<div class="pb-4">Traefik v2</div> <div class="pb-4">Traefik v2</div>
@elseif ($server->proxyType() === 'CADDY') @elseif ($server->proxyType() === 'CADDY')
@ -26,11 +27,8 @@
configurations. configurations.
</div> </div>
@endif @endif
@if ($server->proxyType() === 'TRAEFIK_V2') <x-forms.input placeholder="https://app.coolify.io" id="redirect_url" label="Default Redirect 404"
<x-forms.input placeholder="https://app.coolify.io" id="redirect_url" helper="All urls that has no service available will be redirected to this domain." />
label="Default Redirect 404"
helper="All urls that has no service available will be redirected to this domain." />
@endif
<div wire:loading wire:target="loadProxyConfiguration" class="pt-4"> <div wire:loading wire:target="loadProxyConfiguration" class="pt-4">
<x-loading text="Loading proxy configuration..." /> <x-loading text="Loading proxy configuration..." />
</div> </div>