-
-
Save Nilpo/e88d9d43623876921854d38851cd37e8 to your computer and use it in GitHub Desktop.
Enabling HTTPS (SSL) for Laravel Sail using Caddy
This file contains 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 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 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 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 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 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
To use this method, create a new Laravel 9 project using the online builder.
curl -s https://laravel.build/caddy-laravelsail | bash && cd caddy-laravelsail
Create a controller to handle the Caddy domain verification route.
php artisan make:controller CaddyProxyController
Create or edit the listed files as shown above.
Install the Laravel dependencies, including Laravel Sail.
docker run --rm --interactive --tty -v $(pwd):/app composer install --ignore-platform-reqs
Build and run the Docker containers using Laravel Sail.
./vendor/bin/sail up --build --remove-orphans -d
(Optional) Run database migrations.
./vendor/bin/sail artisan migrate
(Optional) Install NPM dependencies and run the Vite dev script.
./vendor/bin/sail npm -i && ./vendor/bin/sail npm run dev
Visit the project URL in the browser.
https://laravel.test
A fully integrated example can be found at the following repository.
https://github.com/Nilpo/caddy-laravelsail.git