I've got a Node.js powered site that I'm running on Amazon Elastic Beanstalk.
My Node.js app listens on port 8080, and I'm using the nginx elastic load balancer configuration with my EB app, listening on port 80 and 443 for HTTP and HTTPS.
However, I only want to accept traffic in my app that has come via HTTPS.
I could rig something up in the app to deal with this, but am interested in a way to get the load balancer to redirect all HTTP requests to my site via HTTPS.
I was able to get this working with a slightly simpler solution.
Please note, this is an elastic beanstalk deployed SINGLE instance, not load balenced.
This was my ebextension I added.
After several false-starts with ideas from Amazon's paid support, they did come through in the end. The way you get this to work is you configure your environment to respond to both port 80 and 443. Then create a folder in your main Node.js app folder called
.ebextensions
, and you place a file named00_nginx_https_rw.config
in there, with this text as the contents:Amazon's support team explained: This config creates a deployment hook which will add the rewrite rules to /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf.
(Previously they had offered me .config's that copied separate files into /etc/nginx/conf.d, but those either had no effect, or worse, seemed to overwrite or take precedence over the default nginx configuration, for some reason.)
If you ever want to undo this, i.e. to remove the hooks, you need to remove this ebextension and issue a command to remove the files that it creates. You can do this either manually, or via ebextensions commands you put in place temporarily:
I haven't tried this, but presumably something like this would work to remove them and undo this change:
Hope this can help someone else in the future.
I'm working with Elastic Beanstalk and Docker, so have taken a slightly different route to get things running for me, but very much inspired by the accepted answer. This script injects required config into /etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf. (If anyone has a more elegant solution would love to see it)
This script also enables the Beanstalk healthcheck to hit my healthcheck endpoint (in my case api/healthcheck) Better to allow the LoadBalancer to hit the app, rather than terminate at Nginx.
I was able to get this to work in a different way. I changed my load balancer to forward port 80 traffic to port 8082, and changed the firewall rules (inbound on the instance, outbound on the firewall) to allow that. And then added this file in .ebextensions:
You could handle the redirect via your Node.js app.
Amazon sends the
X-Forwarded-Proto
header which equalshttp
when the client has connected insecurely.The following middleware should be inserted right after initializing
Express
and before defining your routes to automatically redirect the client to the corresponding HTTPS endpoint:The accepted answer no longer worked for me. The default port was a different one. Also the location of the config file has changed. I am setting up a Ruby On Rails application with Puma.
I talked to the paid support, we figured it out by just running the commands manually on the running instance. Then I was able to figure out the below solution. Just by logging in and restarting nginx things then worked.
Notice how I changed the port number and the location of the config file.