Symfony2:getScheme 不 return 'https'
Symfony2: getScheme does not return 'https'
在一个控制器中,我一直在使用方法 getSchemeAndHttpHost 来构建另一个控制器的 url。
一切都 运行 顺利,直到我需要通过 https。我通过 getSchemeAndHttpHost 函数获得的所有 urls 仍然是 http。进行一些故障排除后,发现 getScheme 函数总是 returns http,当我使用 https 和 http 请求页面时。
我是不是漏掉了什么?
我怎么知道我的控制器是通过 http 还是 https 访问的?
编辑 - 更多信息
- 我确保 运行 使用有效证书进行测试。
- 如果我 运行 我的 Request 对象中的 isSecure 方法,我总是得到 false。
简答
在控制器中添加以下行:
Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR')));
更多细节
经过一些调查,我发现这与代理有关...
如果我们检查 isSecure 函数在做什么,我们会发现 this:调用 Request 方法 isFromTrustedProxy 来决定是真还是假应该归还。
一旦我们知道问题与代理有关,symfony 文档会详细解释 what we should do to trust proxies or how to manage a load balancer or a reverse proxy. In my case, as I am working with heroku, trusting the incoming ip 是解决之道。
在一个控制器中,我一直在使用方法 getSchemeAndHttpHost 来构建另一个控制器的 url。
一切都 运行 顺利,直到我需要通过 https。我通过 getSchemeAndHttpHost 函数获得的所有 urls 仍然是 http。进行一些故障排除后,发现 getScheme 函数总是 returns http,当我使用 https 和 http 请求页面时。
我是不是漏掉了什么?
我怎么知道我的控制器是通过 http 还是 https 访问的?
编辑 - 更多信息
- 我确保 运行 使用有效证书进行测试。
- 如果我 运行 我的 Request 对象中的 isSecure 方法,我总是得到 false。
简答
在控制器中添加以下行:
Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR')));
更多细节
经过一些调查,我发现这与代理有关... 如果我们检查 isSecure 函数在做什么,我们会发现 this:调用 Request 方法 isFromTrustedProxy 来决定是真还是假应该归还。
一旦我们知道问题与代理有关,symfony 文档会详细解释 what we should do to trust proxies or how to manage a load balancer or a reverse proxy. In my case, as I am working with heroku, trusting the incoming ip 是解决之道。