Elastic Beanstalk HTTPS redirection when using TCP ports
I’m deploying an application to a Docker environment within Elastic Beanstalk. The application serves HTTP and Web Socket requests over the HTTP(s) port.
AWS provides a configuration that works when forwarding HTTP to HTTPS. However, in order to serve web socket requests, I needed to forward SSL to TCP in the load balancer configuration.
When using the provided configuration https and https request would get redirect to https. This lead to an infinite loop. The snippet below was the cause.
The protocol being forwarded was not https, so a redirect would be returned until the browser stopped following them. I adjusted the configuration to look for positive matches specific request protocols.
The logic may look a little strange, but nginx doesn’t allow for compound Boolean logic. Essentially ws
is redirected to wss
and http
is redirected to https
unless the request is a health check.