Warm tip: This article is reproduced from serverfault.com, please click

apache-反向代理背后的 Laravel 路由

(apache - Laravel routes behind reverse proxy)

发布于 2015-04-28 07:29:04

好的,出于开发目的,我们有一个专用的 Web 服务器。它目前没有直接连接到互联网,所以我在另一台服务器上设置了一个 apache 反向代理,它转发到开发服务器。

这样,我就可以通过网络访问服务器。

问题是,Laravel 中的路由现在以内部服务器 IP 地址或服务器计算机名称作为前缀。

例如,我转到http://subdomain.test.com,但使用route()帮助程序生成的所有路由都显示以下 url:http://10.47.32.22而不是http://subdomain.test.com

反向代理设置如下:

<VirtualHost *:80>
    ServerName igateway.somedomain.com

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass / http://10.47.32.22:80/
    ProxyPassReverse / http://10.47.32.22:80/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
</VirtualHost>

我已经在config\app.php.

问题

如何设置要在路由中使用的默认 URL?我不希望它使用内部地址,因为这违背了反向代理的意义。

我试过将我的所有路线都包含在一个Route::group(['domain' ...组中,这也不起作用。

Questioner
Phil Cross
Viewed
0
2,645 2020-02-10 12:11:33

当 Laravel 5 应用程序不知道位于 SSL 负载平衡器后面时,我遇到了相同(或类似的问题)。

我有以下设计:

  • 客户端通过 HTTPS 与 SSL 负载平衡器对话
  • SSL 负载平衡器通过 HTTP 与后端服务器通信

但是,这会导致 HTML 代码中的所有 URL 都使用 http:// 模式生成。

以下是使这项工作的快速解决方法,包括架构(http 与 https):

将以下代码放在app/Http/routes.php之上

在最新版本的 Laravel 中,使用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);
}

然后将以下行添加到.env文件中:

PROXY_URL = http://igateway.somedomain.com

如果你还需要将生成的 HTML 代码中的架构从http://更改为https://,只需添加以下行:

PROXY_SCHEMA = https

在最新版本的 LaravelforceSchema方法名称已更改为forceScheme,上面的代码应如下所示:

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