duanre1891 2015-02-13 09:34
浏览 153
已采纳

Laravel配置力量https

Our website is hosted on a load balanced server. SSL offloading is done on the firewall, so the client is reverse proxied to the web server farm.

When the https request reaches our Laravel app, the server variable HTTPS is empty and Laravel doesn't seem to detect the https mode and generates urls (assets & routes) as:

Is there a way to configure Laravel to force the url's to generate https links? We prefer to have a configuration solution because we have a development and staging environment that aren't running under https.

Notice: We already tried the "trustedproxy" approach from fideloper, and this resulted in no change. I presume that a .htaccess rewrite is not an option since htaccess rewrites are based on the same https header (we don't receive) or port (80, laravel calls port 443).

Thanks for the help.

  • 写回答

2条回答 默认 最新

  • dtu15253 2015-02-13 11:35
    关注

    Laravel's UrlGenerator class has a method called forceSchema, which allows you to force the schema to be used and ignore the one extracted from the request's URL. Just create a service provider SecureRoutingServiceProvider which uses Laravel's IOC to override the default generator and return an instance that forces the secure schema:

    use Illuminate\Routing\UrlGenerator;
    use Illuminate\Routing\RoutingServiceProvider;
    
    class SecureRoutingServiceProvider extends RoutingServiceProvider
    {
        public function boot()
        {
            App::bind('url', function () {
                $generator = new UrlGenerator(
                    App::make('router')->getRoutes(),
                    App::make('request');
                });
    
                $generator->forceSchema('https');
    
                return $generator;
            }
    
            parent::boot();
        }
    }
    

    Next we'll need to register the service provider by adding it to the providers array in app/config/app.php:

    'providers' => array(
        ...,
        'SecureRoutingServiceProvider',
    )
    

    And that's all there is to it. I've tested this code and it works fine (in Laravel 4.2).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程