-
-
Save tazeverywhere/7c20bac240974e3826fefb1d4ec7c512 to your computer and use it in GitHub Desktop.
Enabling HTTPS (SSL) for Laravel Sail using Caddy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APP_URL=https://laravel.test | |
APP_SERVICE=laravel.test | |
[...] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'), | |
[...] | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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