Symfony2: getScheme does not return 'https'

半腔热情 提交于 2019-12-12 09:39:19

问题


From within a controller, I have been using the method getSchemeAndHttpHost in order to build the url of another controller.

Everything has been running smoothly, until I needed to go over https. All urls that I get with the getSchemeAndHttpHost function are still http. Doing some troubleshooting, it came out that the getScheme function always returns http, both when I request the page with https and with http.

Did I miss something ?

How can I know if I am over http or https from my controller ?

Edit - some more information

  1. I made sure to run the tests with a valid certificate.
  2. If I run the isSecure method in my Request object, I always get false.

回答1:


Short answer

Add the following line in the controller:

Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR')));

Some more details

After some investigations, I discovered that this has to do with Proxies... If we check what the isSecure function is doing, we find this: the Request method isFromTrustedProxy is called to decide if true or false should be returned.

Once we know the issue has to do with proxies, the symfony documentation explains in details 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 was the way to go.



来源:https://stackoverflow.com/questions/33436177/symfony2-getscheme-does-not-return-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!