Skip to content

Instantly share code, notes, and snippets.

@tazeverywhere
Forked from Nilpo/.env
Created June 14, 2024 13:43
Show Gist options
  • Save tazeverywhere/7c20bac240974e3826fefb1d4ec7c512 to your computer and use it in GitHub Desktop.
Save tazeverywhere/7c20bac240974e3826fefb1d4ec7c512 to your computer and use it in GitHub Desktop.
Enabling HTTPS (SSL) for Laravel Sail using Caddy
APP_URL=https://laravel.test
APP_SERVICE=laravel.test
[...]
<?php
# config/app.php
[...]
/*
|--------------------------------------------------------------------------
| Application Service
|--------------------------------------------------------------------------
|
| The APP_SERVICE environment variable is used when the default docker
| service name is changed from its default 'laravel.test' value.
| Laravel Sail will fail to start if there is a mismatch.
|
*/
'service' => env('APP_SERVICE', 'laravel.test'),
[...]
];
# ./Caddyfile
{
on_demand_tls {
ask http://laravel.test/domain-verify
}
local_certs
}
:443 {
tls internal {
on_demand
}
reverse_proxy laravel.test {
header_up Host {host}
header_up X-Real-IP {remote}
header_up X-Forwarded-For {remote}
header_up X-Forwarded-Port {server_port}
header_up X-Forwarded-Proto {scheme}
health_timeout 5s
}
}
<?php
# app/Http/Controllers/CaddyProxyController.php
# > php artisan make:controller CaddyProxyController
namespace App\Http\Controllers;
use App\Store;
use Illuminate\Http\Request;
class CaddyProxyController extends Controller
{
public function verifyDomain(Request $request)
{
$authorizedDomains = [
config('app.service'), // laravel.test
'localhost',
// Add subdomains here
];
if (in_array($request->query('domain'), $authorizedDomains)) {
return response('Domain Authorized');
}
// Abort if there's no 200 response returned above
abort(503);
}
}
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test
[...]
ports:
# - '${APP_PORT:-80}:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
[...]
caddy:
image: caddy:latest
restart: unless-stopped
ports:
- '${APP_PORT:-80}:80'
- '${APP_SECURE_PORT:-443}:443'
volumes:
- './Caddyfile:/etc/caddy/Caddyfile'
- sail-caddy:/data
- sail-caddy:/config
networks:
- sail
[...]
volumes:
[...]
sailcaddy:
driver: local
<?php
# routes/web.php
[...]
use App\Http\Controllers\CaddyProxyController;
[...]
Route::get('/domain-verify', [CaddyProxyController::class, 'verifyDomain')];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment