Dan Bolofe Dan Bolofe - 1 year ago 385
Apache Configuration Question

Configure apache on elastic beanstalk

I'm developing with django on elastic beanstalk and I want to make two changes to apache configuration:

redirect www.domain.com to domain.com

redirect http://domain.com to https://domain.com

I don't have experience with apache configuration, googling it gave me the idea that I should put RewriteRules in .htaccess file.

How to force https on amazon elastic beanstalk without failing the health check

I couldn't find instructions on how to do it with elastic beanstalk configuration (.ebextensions), I tried to simply put a .htaccess file in my root filder and deploy but it didn't work.

Does anyone know how it's possible to add the RewriteRules in elastic beanstalk?

Answer Source

this is an easy solution

  1. ssh into your EC2 instance
  2. copy the contents of /etc/httpd/conf.d/wsgi.conf into a local file called wsgi.conf which will be placed in the base folder of your application
  3. Edit the local version of wsgi.conf and add the following redirect rules within the < VirtualHost> < /VirtualHost> tags

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
  4. Change the “/status” to whatever page you are using as a health check page.

  5. Save the file
  6. Edit your < app>.conf file inside your .ebextensions directory to add a container command to copy this version of wsgi.conf over Amazon’s version

      command: "django-admin.py syncdb --noinput" leader_only: true
      command: "django-admin.py collectstatic --noinput"
      command: 'cp wsgi.conf /etc/httpd/conf.d/wsgi.conf'
  7. Deploy the code.

  8. The deployed version of wsgi.conf at /etc/httpd/conf.d/wsgi.conf will now include the necessary redirect rules.

It should work and the file will be properly updated for each deployment. The only thing to watch for is if Amazon changes their base wsgi.conf file contents in the future, then your copy may no longer work.

Source : rickchristianson