Laravel 使用 HTTPS
Laravel Use HTTPS
我有项目并在 ubuntu 服务器上安装了 SSL 证书。我已经完成了我找到的所有解决方案,但每次打开页面时仍然收到此消息。
The requested URL /about was not found on this server.
这是我为强制 Laravel 使用 https:
所做的更改
1- 我更改了 public 文件夹中的 .htaccess 并添加了这些行
# Added to Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
2- 在 app->Providers->AppServiceProvider 中,我已将其添加到 boot() 函数中
if (App::environment() === 'production' || App::environment() === 'dev') {
URL::forceScheme('https');
}
3- 我创建了 php artisan make:middleware ForceSSL 并将以下代码添加到句柄函数
if (!$request->secure() && in_array(env('APP_ENV'), ['stage', 'production'])) {
return redirect()->secure($request->getRequestUri());
}
return $next($request);
并在 Kernal.php
\MyApp\Http\Middleware\ForceSSL::class
在 .env 文件中,我已将 APP_URL 更改为 https://
我还在配置文件夹中的 app.php 中更改了 APP_URL。
我在这里遗漏了什么?这两天我不明白为什么:(
您需要做一些事情来确保正常工作:
- 您的路由是否在 url 中没有
index.php
的情况下以标准 HTTP 模式工作。就像有时 laravel.com/aboutus 不起作用但 laravel.com/index.php/aboutus 起作用。如果是这种情况,您需要在 php 中启用 mod_rewrites
,然后在您的虚拟主机配置中添加 AllowOverride All
并重新启动 apache。
- 使用 HTTPS,您在 laravel 中设置的内容将使 laravel 请求转发到安全的 HTTPS url。但是,您的服务器必须能够侦听、处理和响应 HTTPS 请求。
- 您需要启用 yoru 虚拟主机来侦听端口
443
,这是 SSL 端口。此外,如果您有 SSL 证书,则还需要配置这些证书。
- 在了解 laravel 是否适用于 HTTPS 之前,制作一个简单的 php 文件并尝试使用服务器 url 的 HTTPS 访问它。如果可行,那么您可以检查 laravel 中的错误。如果没有,那么您的服务器上的 SSL 配置不正确。
- 最后,检查
.htaccess
重写条件。
php artisan config:clear
和 php artisan route:cache
希望这有助于调试这个
AWS |中心 | EC2 | AWS 证书
有点晚了,但我自己只需要在我的环境中改变它。文件:
APP_URL=http://localhost
to
APP_URL=https://localhost
一切顺利。
我有项目并在 ubuntu 服务器上安装了 SSL 证书。我已经完成了我找到的所有解决方案,但每次打开页面时仍然收到此消息。
The requested URL /about was not found on this server.
这是我为强制 Laravel 使用 https:
所做的更改1- 我更改了 public 文件夹中的 .htaccess 并添加了这些行
# Added to Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
2- 在 app->Providers->AppServiceProvider 中,我已将其添加到 boot() 函数中
if (App::environment() === 'production' || App::environment() === 'dev') {
URL::forceScheme('https');
}
3- 我创建了 php artisan make:middleware ForceSSL 并将以下代码添加到句柄函数
if (!$request->secure() && in_array(env('APP_ENV'), ['stage', 'production'])) {
return redirect()->secure($request->getRequestUri());
}
return $next($request);
并在 Kernal.php
\MyApp\Http\Middleware\ForceSSL::class
在 .env 文件中,我已将 APP_URL 更改为 https:// 我还在配置文件夹中的 app.php 中更改了 APP_URL。
我在这里遗漏了什么?这两天我不明白为什么:(
您需要做一些事情来确保正常工作:
- 您的路由是否在 url 中没有
index.php
的情况下以标准 HTTP 模式工作。就像有时 laravel.com/aboutus 不起作用但 laravel.com/index.php/aboutus 起作用。如果是这种情况,您需要在 php 中启用mod_rewrites
,然后在您的虚拟主机配置中添加AllowOverride All
并重新启动 apache。 - 使用 HTTPS,您在 laravel 中设置的内容将使 laravel 请求转发到安全的 HTTPS url。但是,您的服务器必须能够侦听、处理和响应 HTTPS 请求。
- 您需要启用 yoru 虚拟主机来侦听端口
443
,这是 SSL 端口。此外,如果您有 SSL 证书,则还需要配置这些证书。 - 在了解 laravel 是否适用于 HTTPS 之前,制作一个简单的 php 文件并尝试使用服务器 url 的 HTTPS 访问它。如果可行,那么您可以检查 laravel 中的错误。如果没有,那么您的服务器上的 SSL 配置不正确。
- 最后,检查
.htaccess
重写条件。 php artisan config:clear
和php artisan route:cache
希望这有助于调试这个
AWS |中心 | EC2 | AWS 证书
有点晚了,但我自己只需要在我的环境中改变它。文件:
APP_URL=http://localhost
to
APP_URL=https://localhost
一切顺利。