Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

https redirection loop even with X-Forwarded-Proto

We are building a new server with : Pound -> Varnish -> Apache -> CentOS.

Since Varnish doesn't work in SSL we are setting "X-Forwarded-Proto" to "https" in Pound and we are detecting that way if we are in https.

It's working when we access directly a url like https://example.com but not when we do a redirection from "http" to "https" with "htaccess" or "PHP". It's seem like the X-Forwarded-Proto isn't forwarded with the redirection. So we get stuck in an infinite redirection loop.

We have found a way to perform the redirection with javascript but we would prefer to have a server side solution.

So we wondering if there is a setting to change in apache, pound, varnish, etc. ?

We have tried a lot of solutions like:

////////////////
// htaccess
////////////////////
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule (.*) https://example.com [L,R]


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

if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on'){
    header('Location: '. 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}

Our pound config look like:

//////////////////
// pound
///////////////
ListenHTTPS

      Address 0.0.0.0 # all interfaces
      Port 443
      AddHeader "X-Forwarded-Proto: https"
      HeadRemove "X-Forwarded-Proto"
      HeadRemove "X-Forwarded-For"
      Cert "/path/to/certificate.pem

      Service
            BackEnd
                  Address 10.0.0.1
                  Port 80
                  Priority 1
            End

      End
End

We have passed a lot of time on that problem thanks to help us!

like image 646
JPR Avatar asked Aug 20 '26 09:08

JPR


1 Answers

As noted above:

We had to:

  • Put RewriteLocation 0 in the ListenHTTPs
  • Fix a domain name issue in the config
ListenHTTPS

  ReWriteLocation 0
like image 192
2 revsPaul Sweatte Avatar answered Aug 21 '26 23:08

2 revsPaul Sweatte