【问题标题】:Setting up HTTPS redirects on Heroku Laravel Instance在 Heroku Laravel 实例上设置 HTTPS 重定向
【发布时间】:2015-10-06 16:55:50
【问题描述】:

我有一个在 apache Heroku 实例上运行的 Laravel 5 构建,我正在努力确保所有流量都通过 https 进行转换,但是我非常迷茫。

我已成功启动并运行 SSL 证书。但是,使用 Heroku,您无法直接在其服务器上编辑 .htaccess 文件。所以他们对this page 的建议是设置一个 apache_app.conf 并告诉 Heroku 通过将其放入 Procfile 来读取它:

web: vendor/bin/heroku-php-apache2 -C apache_app.conf public/

但是,当我这样做时,我添加的几乎任何类型的 HTTPS 重写规则都会导致重定向循环或应用程序错误。这是我目前的配置:

RewriteEngine on

#Normal way (in case you need to deploy to NON-heroku)
RewriteCond %{HTTPS} !=on

#Heroku way
RewriteCond %{HTTP:X-Forwarded-Proto} !https

#If neither above conditions are met, redirect to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

【问题讨论】:

  • 您可以将您的apache_app.conf 内容添加到您的问题中吗?
  • @elithrar 当然,更新了。我应该注意到,我终于能够通过将“DirectoryIndex index.php index.html index.htm”添加到 apache_conf.php 文件的顶部来解决这个问题。我想这就是问题所在。虽然我承认我对这些东西没有深入了解。
  • 您应该回答自己的问题,然后将其标记为答案。这完全解决了我的问题。谢谢。
  • 将该代码添加到 public/.htaccess 文件并从 Procfile 中删除“-C apache_app.conf”也可以解决问题

标签: php apache laravel ssl heroku


【解决方案1】:

我可以通过this 方法而不是通过.htaccess 处理重定向

HttpsProtocol 中间件会将每条网络路由重定向到 HTTPS 协议

【讨论】:

  • 我在 api.php 和 web.php 中定义的路由在 heruko 中不起作用
  • return redirect()->secure() 在我的 heroku 实例上不起作用。知道为什么吗?
【解决方案2】:

根据 Heroku 文档,您必须将“TrustProxies”中间件修改为将 $proxies 设置为“*”

class TrustProxies extends Middleware
{
    protected $proxies = '*';
// ...

https://devcenter.heroku.com/articles/getting-started-with-laravel#trusting-the-load-balancer

【讨论】:

    猜你喜欢
    • 2021-04-11
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2014-05-14
    • 2021-08-31
    • 2017-03-26
    • 2018-03-03
    相关资源
    最近更新 更多