filhocodes/laravel-sail-ssl-proxy

为 Sail 环境添加 SSL 的简单解决方案

v1.0.0 2022-02-23 00:32 UTC

This package is auto-updated.

Last update: 2024-09-23 06:24:04 UTC


README

Composer Package Composer Package Downloads

一种简单且现成的配置代理的方法,允许您通过 HTTPS 使用 Laravel Sail 主办本地项目。

SSL 代理的设计借鉴自 https://dev.to/adrianmejias/how-to-enable-ssl-for-local-development-using-laravel-sail-and-docker-51ee。基本上,Caddy 将在 Laravel 服务器之上执行,并生成一个本地证书。然后您可以将此证书添加到您的本地受信任存储中。

快速使用

如果您没有本地安装 PHP 和 Composer,您可以使用此别名通过 docker 执行这两个工具

alias sail81 = 'mkdir -p $HOME/.sail/81/.composer/ && docker run --rm -it -u "$(id -u):$(id -g)" -v $HOME/.sail/81/.composer:/.composer -v $(pwd):/opt -w /opt laravelsail/php81-composer:latest'

然后,您只需在下面的命令前加上 sail81

# Running composer
sail81 composer {...}

# Running an artisan command
sail81 php artisan {...}

在设置代理之前,Laravel Sail 应已在您的项目中配置。使用 Laravel 文档 提供的脚本进行的安装将已经有一个 Laravel Sail 环境。除此之外,您还需要自行配置它。请参阅 Laravel Sail 文档 以进行配置。

使用 Laravel Sail 的项目,使用 Composer 需要此包

composer require --dev filhocodes/laravel-sail-ssl-proxy

然后使用以下命令安装代理服务

php artisan ssl-proxy:install -m

# -m will write a logic into app/Http/Middleware/TrustProxies.php to trust the SSL proxy.
# You may update this logic as you wish, or you can omit the option so that the file is not modified.
# In either case, is recommended in development to trust the reverse proxy

之后,启动开发环境

./vendor/bin/sail up -d

# If you are using our custom logic to trust the SSL proxy, after the containers are booted up, you may use the
# following command to retrieve the SSL proxy IP (will not work on Windows):
#     ./vendor/bin/filhocodes-ssl-proxy-env

在向应用程序发出第一个 HTTP 请求后,将在 docker/sail-ssl-proxy/authorities/intermediate.crt 创建证书。然后您可以将此证书添加到您的受信任存储中。在 Linux 上,您可以使用命令 ./vendor/bin/filhocodes-ssl-proxy-trust 将证书添加到系统存储。

如果已生成授权证书,但 certificates 目录为空,这通常意味着授权流程失败。这主要可能是由于域名检查。默认情况下,我们通过解析通过 app.url 配置(以及默认的 APP_URL 环境变量)定义的应用程序 URL 的域名。如果需要,您可以发布我们的配置并更新 authorized_domains 配置中的值。

php artisan vendor:publish --tag=filhocodes-ssl-proxy-config

您还可以设置环境变量 FILHOCODES_LARAVEL_SAIL_SSL_PROXY_DEBUG=true 以记录发送到授权控制器的请求。