1

I really tried many Solutions to get my site work on Amazon AWS. There is only one last thing i can not solve. Some of my links are still "http://"-links and i dont know how to get them right.

My Setup:

  • Drupal 8.6.10 running on an EC2-Instance managed via Beanstalk
  • the Load-Balancer is managing the access and has two open ports (http/80 and https/443) directing to the ec2-instance via http (port 80)
  • the ssl-certificate is configured
  • my domain has the correct dns-config directing to xxx.eu-central-1.elasticbeanstalk.com
  • settings.php includes the following lines
     $settings['reverse_proxy'] = TRUE;
     $settings['reverse_proxy_addresses'] = array($_SERVER['PROXY_ADDRESS']);
     $settings['reverse_proxy_proto_header'] = 'https';
     conf['https'] = TRUE;
     $_SERVER['HTTPS']='on';
  • $_SERVER['PROXY_ADDRESS'] is assgned to 'xxx.eu-central-1.elasticbeanstalk.com' by beanstalk
  • .htaccess includes the following lines:
     RewriteCond %{HTTPS} off
     RewriteCond %{HTTP:X-Forwarded-Proto} !https
     RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The affected places are:

  • in a twig: url('')
  • in a php-file: fileentities->url();

All other links are nicely rendered as 'https'-links. I would be really thankfull for any advice you could give me.

Best Regards,

FirstSanny

  • 1
    Set the base URL in settings.php. htaccess should also be forcing every request to https. – Kevin Mar 1 '19 at 16:37
  • Already tried that. But i removed it, because of various posts saying this is no longer supported. But it didn't work anyway. – FirstSanny Mar 1 '19 at 16:39
  • You need to debug the request headers your Drupal install actually is receiving. It's unlikely it is receiving a header named https, this is normally the value. To reduce the affected places use relative urls whenever possible, in Twig path() instead of url(). – 4k4 Mar 1 '19 at 16:59
  • @4k4 thanks for your answer i'll first change my code the way you mentioned it and then i'll try to debug the headers even though it don't know exactly how. But thats something i can find out on my own. Will get back here afterwards. – FirstSanny Mar 1 '19 at 17:05
  • You can dump \Drupal::request()->server->all() – 4k4 Mar 1 '19 at 17:16
0

Try the folling setup:

In settings.php:

if (PHP_SAPI !== 'cli' && !empty($_SERVER['REVERSE_PROXY_ADDRESSES'])) {

  $settings['reverse_proxy'] = TRUE;
  $settings['reverse_proxy_addresses'] = array($_SERVER['REVERSE_PROXY_ADDRESSES']);

  $settings['reverse_proxy_header'] = 'X-Forwarded-For';
  $settings['reverse_proxy_proto_header'] = 'X_Forwarded-Proto';
  $settings['reverse_proxy_port_header'] = 'X-Forwarded-Port';  

  // HTTPS behind reverse-proxy
  if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
    $_SERVER['HTTPS'] = 'on';
    // This is hardcoded because there is no header specifying the original port.
    $_SERVER['SERVER_PORT'] = 443;
  }
}

In your .htaccess:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
#RewriteCond %{HTTP_HOST} !^(.*\.)*localhost(.*)$ [NC]
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

(change $_SERVER['REVERSE_PROXY_ADDRESSES'] to $_SERVER['PROXY_ADDRESS'] for your setup)

This worked fine for me.

| improve this answer | |
  • Thanks for your reply. Sadly that didn't changed anything on my side. The effects stayed the same. Everything is https expect file->url() in php and url(<front>) in twigs. – FirstSanny Mar 4 '19 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.