Symfony2 behind ELB is redirecting to http instead of https

后端 未结 7 1490
予麋鹿
予麋鹿 2021-02-01 17:04

Issue:

  • User logs in with https://example.com/login
  • Authentication is approved
  • As configured in security.yml Symfony2 redirects u
7条回答
  •  无人共我
    2021-02-01 17:50

    I had the exact same problem with a PHP application using AWS and ELB with SSL in a CakePHP application.

    My solution was good in some ways and bad in others. The problem was that Amazon sends different HTTPS headers than the PHP headers you look for: $_SERVER['HTTPS'] is off, while Amazon sends alternative HTTPS headers that you can use to identify that it is in fact running under HTTPS:

    $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'
    

    I worked out that my base URL constant that Cake defined internally had the http protocol in it, so I simply redefined the $_SERVER['HTTPS'] variable on the very first line of my index.php file in Cake - and I wouldn't be surprised if you could do the same in symfony):

    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        $_SERVER['HTTPS'] = 'on';
    }
    

    This allowed my application to continue on, detect HTTPS as being 'on' as would normally be expected and allow Cake to internally manage the protocol in my base URL constant.

    Good:

    • fixed the problem immediately
    • used 3 lines of code

    Bad:

    • whenever I upgrade my Cake core, I'll have to put this back in again

提交回复
热议问题