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

This way, I can get web access to the server.

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

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

The reverse proxy is setup as such:

<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.

Question

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

我试着把我所有的路由围成一个Route::group(['domain' ...人的小组,这也不管用.

推荐答案

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

我有以下设计:

  • client talks to an SSL load balancer over HTTPS
  • SSL负载平衡器通过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):

将以下代码放在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);
}

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

PROXY_URL = http://igateway.somedomain.com

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

PROXY_SCHEMA = https

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

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

Laravel相关问答推荐

我的共享主机上的每个帖子请求都出现 503 服务不可用

如何仅将日期列解析为月和日?

Laravel 验证 - 不同的属性规范

Laravel - 出于某种原因,max 打破了我的查询

Laravel 5.1 - 如何在进度条上设置消息

允许的内存大小 134217728 字节用尽(试图分配 20480 字节) Laravel

Laravel 4 如何监听模型事件?

使用 Eloquent (Laravel) 在 Group By 之前排序

Laravel 5.3 - 将多个文件附加到 Mailables

laravel 5中的配置缓存导致找不到视图

当表中不存在列时如何将 paginate() 与 having() 子句一起使用

Laravel - 更新时禁用更新时间

Laravel 迁移命名约定

Laravel / Eloquent内存泄漏重复检索相同的记录

如何在 Laravel 中使用补丁请求?

在 Laravel 中结合 AND/OR Eloquent的查询

Eloquent - 带有字符串值而不是列标题的连接子句

如何在 Eloquent Orm 中实现自引用(parent_id)模型

Laravel:array_merge():参数#2不是数组错误

在 Laravel 中判断会话超时