【问题标题】:https not working with Slimhttps 不适用于 Slim
【发布时间】:2018-08-29 16:29:46
【问题描述】:

我最近使用 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,这似乎是一个不错的选择,但我不知道从哪里开始。

【问题讨论】:

    标签: https slim middleware


    【解决方案1】:

    我会稍微简化一下.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);
    });
    

    【讨论】:

    • 您好,感谢您的回复,我用您的建议替换了我的 .htaccess 内容并将中间件代码添加到我的 index.php 文件中,但仍然无法正常工作,但我确实收到了一个新错误:这页面不工作... http 错误 500
    • 500 是一个非常普遍的错误。要查看发生了什么,请将 displayErrorDetails 设置为 true。 Details
    • 好的,明天就做。谢谢你。
    • 你好,包含 $config['displayErrorDetails'] = true; 的文件在哪里? ???谢谢
    • 太棒了!一切正常,而且比以往任何时候都好。有一些愚蠢的问题,因为我是服务器端编程的菜鸟,但通过了教程和 BAM!即使在中间件中没有任何代码(这有点奇怪),我也可以通过 https 获取所有数据,但无论如何它都可以正常工作。非常感谢您,先生,您帮助结束了我最后几天的地狱。一切顺利。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2013-09-24
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多