https 不适用于 Slim

https not working with Slim

我最近用 SSL 设置了我的 EC2 LAMP 服务器,它与 Slim 配合得很好。我可以安全地登录到 phpMyAdmin 并查看我的数据。但是,我在连接到 mysql 的路由上收到 504 网关超时错误。当相同的 index.php 文件位于我的本地 XAMPP 服务器(未启用 SSL)时,我的路由工作正常。所以在我看来,https 协议尚未在 LAMP 服务器上运行。环顾四周,我看到了有关修改我的 .htaccess 文件以重写 https:

的事情
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]`

但这没有任何区别,我也不知道为什么。中间件也出现了,将 http 重定向到 https,这似乎是一个不错的选择,但我不知道从哪里开始。

我会稍微简化 .htaccess 文件并将 SSL 检查和重定向移动到中间件。

.htaccess

的内容
# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

中间件

// Redirect HTTP traffic to HTTPS
$app->add(function (Request $request, Response $response, $next) {
    $uri = $request->getUri();
    if($uri->getScheme() !== 'https') {
        // Map http to https
        $httpsUrl = $uri->withScheme('https')->withPort(443)->__toString();

        // Redirect to HTTPS Url
        return $response->withRedirect($httpsUrl);
    }

    return $next($request, $response);
});