Laravel routes behind reverse proxy

后端 未结 5 1467
野趣味
野趣味 2020-12-08 22:16

Ok, so for development purposes, we have a dedicated web server. It\'s not currently connected directly to the internet, so I\'ve setup an apache reverse proxy on another se

5条回答
  •  被撕碎了的回忆
    2020-12-08 22:52

    I ran into the same (or similar problem), when a Laravel 5 application was not aware of being behind an SSL load-balancer.

    I have the following design:

    • client talks to an SSL load balancer over HTTPS
    • SSL load balancer talks to a back-end server over HTTP

    That, however, causes all the URLs in the HTML code to be generated with http:// schema.

    The following is a quick'n'dirty workaround to make this work, including the schema (http vs. https):

    Place the following code on top of app/Http/routes.php

    In latest version of laravel, use web/routes.php

    $proxy_url    = getenv('PROXY_URL');
    $proxy_schema = getenv('PROXY_SCHEMA');
    
    if (!empty($proxy_url)) {
       URL::forceRootUrl($proxy_url);
    }
    
    if (!empty($proxy_schema)) {
       URL::forceSchema($proxy_schema);
    }
    

    then add the following line into .env file:

    PROXY_URL = http://igateway.somedomain.com
    

    If you also need to change schema in the generated HTML code from http:// to https://, just add the following line as well:

    PROXY_SCHEMA = https
    

    In latest version of laravel forceSchema method name has changed to forceScheme and the code above should look like this:

    if (!empty($proxy_schema)) {
        URL::forceScheme($proxy_schema);
    }
    

提交回复
热议问题