Skip to content

Instantly share code, notes, and snippets.

@Nilpo
Forked from gilbitron/CaddyController.php
Last active December 7, 2022 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nilpo/e88d9d43623876921854d38851cd37e8 to your computer and use it in GitHub Desktop.
Save Nilpo/e88d9d43623876921854d38851cd37e8 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')];
@Nilpo
Copy link
Author

Nilpo commented Oct 23, 2022

@Nilpo
Copy link
Author

Nilpo commented Dec 3, 2022

  1. 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

  2. Create a controller to handle the Caddy domain verification route.

    php artisan make:controller CaddyProxyController

  3. Create or edit the listed files as shown above.

  4. Install the Laravel dependencies, including Laravel Sail.

    docker run --rm --interactive --tty -v $(pwd):/app composer install --ignore-platform-reqs

  5. Build and run the Docker containers using Laravel Sail.

    ./vendor/bin/sail up --build --remove-orphans -d

  6. (Optional) Run database migrations.

    ./vendor/bin/sail artisan migrate

  7. (Optional) Install NPM dependencies and run the Vite dev script.

    ./vendor/bin/sail npm -i && ./vendor/bin/sail npm run dev

  8. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment