Redirect to Https using Elastic Beanstalk ELB

2019-08-12 17:45发布

问题:

What I want to accomplish is to redirect all traffic from http to https. Basically I have a balanced instance, with both http and https working. (The https is working fine, the certificate has been setted up), and I have created a directory .ebextensions in my .war application with a config file inside as following.

    files: 
  /etc/httpd/conf.d/vhosts.conf: 
    content: |
        <VirtualHost *:80>
          RewriteCond %{HTTP:X-Forwarded-Proto} !=https
          RewriteRule ^/(.*)$ https://www.mydomain.com/$1 [R=301,L]
        </VirtualHost>
    group: root
    mode: "000644"
    owner: root
services: 
  sysvinit: 
    httpd: 
      enabled: true
      ensureRunning: true
      files: 
        - /etc/httpd/conf.d/vhosts.conf

The logs ...

2014-05-16 01:27:48,253 [DEBUG] /etc/httpd/conf.d/vhosts.conf already exists
2014-05-16 01:27:48,254 [DEBUG] Moving /etc/httpd/conf.d/vhosts.conf.bak to /etc/httpd/conf.d/vhosts.conf.bak2
2014-05-16 01:27:48,254 [DEBUG] Moving /etc/httpd/conf.d/vhosts.conf to /etc/httpd/conf.d/vhosts.conf.bak
2014-05-16 01:27:48,254 [DEBUG] Writing content to /etc/httpd/conf.d/vhosts.conf
2014-05-16 01:27:48,254 [DEBUG] Setting mode for /etc/httpd/conf.d/vhosts.conf to 000644
2014-05-16 01:27:48,254 [DEBUG] Setting owner 0 and group 0 for /etc/httpd/conf.d/vhosts.conf
2014-05-16 01:27:48,255 [DEBUG] Moving /etc/httpd/conf.d/vhosts.conf.bak2 to /etc/httpd/conf.d/vhosts.conf.bak
2014-05-16 01:27:48,255 [DEBUG] No commands specified
2014-05-16 01:27:48,255 [DEBUG] Using service modifier: /sbin/chkconfig
2014-05-16 01:27:48,255 [DEBUG] Setting service httpd to enabled
2014-05-16 01:27:48,372 [INFO] enabled service httpd
2014-05-16 01:27:48,372 [DEBUG] Using service runner: /sbin/service
2014-05-16 01:27:48,469 [DEBUG] No need to modify running state of service httpd
2014-05-16 01:27:48,481 [INFO] Running configSet Hook-PreAppDeploy

Checking the logs I'm seeing that the script is being executed, and the changes applied, but I'm not seeing the service being restarted. Could that be the reason on my problem? Or do I have another error in my script file?

Thanks a lot for your help.

回答1:

I was able to make it work configuring the file elasticbeanstalk.conf, adding this configuration.

  RewriteEngine On
  RewriteCond %{HTTP:X-Forwarded-Proto} !=https
  RewriteRule ^/(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]