Using Laravel routes in reverse proxy

Reverse proxy server is a kind of server usually exists behind firewall of a private network. It leads any client request to the right server on the backend. It is also used to cache content and compressing inbound and outbound data which results in a fast and easy flow of traffic between client and servers. It has also a significant role in reducing the load on web servers and it handles SSL encryption. Now coming to the title. When using reverse proxy in most of the cases the routes in Laravel get prefixed with internal server IP address or the servers computer name. This just betrays the purpose and point of using reverse proxy. In your case if the client communicates to an SSL load balancer over HTTPS and SSL load balancer communicate back to back-end server over HTTP. This will causes all URLs in the HTML code to be generated with http://schema. Add the following code on top of app/Http/route.php or if you are using the latest version of Laravel, in 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);

}


In .env file add the following.


PROXY_URL = http://igateway.somedomain.com


Add the following line if you want to change the schema also.


PROXY_SCHEMA = https


For latest version:


if(!empty($proxy_schema)) {

URL::forceScheme($proxy_schema);

}