【问题标题】:Laravel on Heroku enforces HTTPHeroku 上的 Laravel 强制执行 HTTP
【发布时间】:2018-11-20 14:48:44
【问题描述】:

我有一个简单的路由,它处理从数据库重定向到正确的跟踪链接。路由本身看起来像这样 -

Route::get('/{language}/go/{operator}', function($language, $operator) {
    $operator = Operator::where('language',$language)->where('name',$operator)->first();
    $tracking_url = $operator->visit_url;
    if( isset($_GET['session_id']) ) $session_id = strip_tags($_GET['session_id']);
    else $session_id = 1;

    if( $operator->dynamic_parameter ) {
        $full_url = $tracking_url.'&'.$operator->dynamic_parameter.'='.$session_id;
    }
    else {
        $full_url = $tracking_url;
    }
    return redirect($full_url)->header('Referrer-Policy', 'no-referrer');
});

整个网站驻留在 HTTPS 上(由 heroku 处理),作为重定向链的额外层,我添加了另外 2 件事 -

1) 添加一些额外的服务器配置 (apache_app.conf) 并使用 procfile 加载它 -

apache_app.conf

DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

过程文件

web: vendor/bin/heroku-php-apache2 -C apache_app.conf public/

2) 强制通过 https 重定向我的所有路由,使用 web.php 顶部的这个 sn-p -

if (env('APP_ENV') === 'prod') {
    \URL::forceScheme('https');
}

在尝试通过 http:// 访问我的应用程序时 - 我 - 正在被重定向到 https:// 版本,看起来没问题。但是当我使用路由的 /en/go/xxxx 端点调查重定向链时,我得到了一些奇怪的链接,其中包括首先重定向回 http 版本,然后将其重定向回 https 版本。因此,当尝试在 iframe 中加载其中一个跟踪 url 时 - 我收到一个错误 -

Mixed Content: The page at 'https://www.website.com/us/reviews/xxxxx' was loaded over HTTPS, but requested an insecure resource 'http://www.website.com/us/go/xxxxx?session_id=1'. This request has been blocked; the content must be served over HTTPS.

重定向链看起来像这样 -

Result
https://www.website.com/us/go/xxxxx/?session_id=332
301 Moved Permanently
http://www.website.com/us/go/xxxxx?session_id=332
301 Moved Permanently
https://www.website.com/us/go/xxxxx?session_id=332
302 Found
https://track.xxxxx.com/visit/?bta=123456&nci=654321&utm_campaign=website_camapign&afp=332

【问题讨论】:

    标签: php laravel heroku laravel-5


    【解决方案1】:

    在您的 apache 配置文件中,确保您的 ServerName 设置为 https://www.website.com 而不是 http://www.website.com

    来自docs

    有时,服务器在处理 SSL 的设备后面运行,例如反向代理、负载平衡器或 SSL 卸载设备。在这种情况下,请在 ServerName 指令中指定客户端连接的 https:// 方案和端口号,以确保服务器生成正确的自引用 URL。

    这可能就是为什么第一个重定向是根据需要使用http 而不是https 生成的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2011-11-03
      相关资源
      最近更新 更多