The site that I am building needs to be secure. So I want all requests to go through HTTPS rather than HTTP. This is simple to do with my normal toolset. Doing this with Django deployed on Amazon’s Elastic Beanstalk was a whole different ordeal. After much Googling, head scratching, and experimenting I finally found a configuration that works.
Here goes:
Phase 1 – Setup your certificate and open the HTTPS ports
- Deploy your Django app to AWS (see Amazon documentation or previous posts for how to do this).
- Create a certificate (self-signed is fine) and use the Amazon IAM tools to upload it. You can find instructions here.
- Open the AWS Elastic Beanstalk admin console
- Click on your application, then click on ‘Configuration‘ in the left hand column
- Click on the gear icon in the ‘Load Balancing” box at the bottom of the page
- In “Secure listening port” select 443
- In “Protocol” below that, select HTTPS
- In “SSL certificate ID” select the certificate you uploaded in step 2.
- In Application health check URL: enter the path to some page in your site that will not be protected (i.e /about)
- You might want to note the Load Balancer ID at this time, you may need it to troubleshoot later.
- Click “Save“
- The instance will restart
At this point your site should work if you type https://<mysite.com> instead of http://<mysite.com>. If it isn’t connecting, then there are a few things to check out:
- Click on “Configuration” again in the left hand column
- Click on the gear icon in the “Instances” box
- Note the “EC2 security groups” id so you can find the same one in the next steps
- Go to the AWS EC2 admin console https://console.aws.amazon.com/ec2/
- Click on “Security Groups” in the left hand column
- Find the security group id from step 3 and click on that
- Click on the “Inbound” tab
- Verify that there is a rule for both 80(HTTP) and 443(HTTPS). If 443 is missing, add it and then click “Apply Rule Changes”
- Wait a minute and check the site again to see if you can access it using https. If so, go to the next phase, otherwise continue here
- Click on “Load Balancers” in the left-hand column.
- Click on the Load Balancer id from Step 10 in the first section
- Click on the “Listeners” tab that appears
- Ensure there is an entry for HTTPS like below. If anything is missing, correct it. Ensure the SSL cert matches the one you uploaded earlier.
Load Balancer Protocol
|
Load Balancer Port
|
Instance Protocol
|
Instance Port
|
Cipher
|
SSL Certificate
|
Actions
|
---|---|---|---|---|---|---|
HTTP
|
80
|
HTTP
|
80
|
N/A
|
N/A
|
|
HTTPS
|
443
|
HTTP
|
80
|
<your cert name>
|
Phase 2 – Configure Django to use HTTPS
By now your site is accessible through https, but it isn’t forcing users to use https. Let’s do those steps now.
First let’s secure Django. In the apps’ Django settings file, add the following:
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True
The two ‘cookie’ settings tell Django not to accept or issue these if the request isn’t secure. This effectively keeps users from signing in or posting data when using HTTP.
The SECURE_PROXY_SSL_HEADER is a little trickier to explain. We setup the AWS Elastic Beanstalk Load Balancer to listen on port 443, but internally it talks to your app on port 80. This can be seen in the Load Balancer configuration above where port 443 redirects to port 80. So traffic coming to your app is ALWAYS on port 80. So how will it know if the user is actually using https? Well, Amazon adds an HTTP header to the request it passes to your app if the original request is using https. The name of that header is “X_Forwarded_Proto”. By setting SECURE_PROXY_SSL_HEADER = (‘HTTP_X_FORWARDED_PROTO’, ‘https’) you’re telling Django to treat any request that contains that header as a https request. Yes, the two headers have different names depending on if it is used in HTTP or in Django.
Go ahead and deploy those changes to the AWS server and wait a few minutes for it to restart.
If all went well, at this point you should be able to access your app on https and log into your app on https. Logging in with http should fail and return the user to the login screen because the session cookie won’t be issued thanks to the settings you just deployed. Posting data using HTTP should also fail for CSRF not being secure (provided your app uses CSRF…and it should)
Phase 3 – Redirect HTTP user to HTTPS
We’re still not done. We want to automatically switch the user to https if they access the app using http. To accomplish that, we have to add some redirect rules to Apache to handle this so there’s one more configuration to change.
The steps below have been replaced with the steps in this post
- ssh into your EC2 instance (see previous posts for how to do this)
- cd /etc/httpd/conf.d/
- sudo vim wsgi.conf
- Add the following within the <VirtualHost>…</VirtualHost> marker
RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule !/about https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
- Change the /about to whatever page you decided to use back in Step 9 in the first section
- Save the file
- Return to your Elastic Beanstalk admin console and select Restart App Server(s) under the ‘Actions” menu.
- The servers will restart
- Go to your site using http://<mysite.com>, the browser should redirect automatically to https://<mysite.com>
Now everything should be working. The only issue will be that wsgi.conf gets overwritten each time you deploy your app. So you have to do Step 4 above each time. I am still working out how to configure that to automatically happen. Stay tuned for an update on that. –UPDATE– New instructions can be found in this post: Updating WSGI automatically in Amazon AWS