Create a gist now

Instantly share code, notes, and snippets.

container_commands:
10redirect:
command: perl -0777 -pe 's#(listen 8080;\n)#$1\n if (\$request_uri = "/health") {\n set \$redirectFlag N;\n }\n\n if (\$http_x_forwarded_proto != "https") {\n set \$redirectFlag "\${redirectFlag}Y";\n }\n\n if (\$redirectFlag = "Y") {\n return 301 https://\$host\$request_uri;\n }#igms' -i /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
# the container command will insert this snippet immediately after
# listen 8080;
# We don't want to redirect health check traffic from the Elastic Load Balancer
# or it will think our app is not healthy
if ($request_uri = "/health") {
set $redirectFlag N;
}
# And nginx doesn't allow nested or compound conditionals
# So we build up this "redirectFlag" variable as a string
if ($http_x_forwarded_proto != "https") {
set $redirectFlag "${redirectFlag}Y";
}
# And now we know: if the value is exactly "Y", it is safe to redirect
# If the traffic is from the load balancer, which we don't want to redirect,
# the value will be "NY", which will not match
if ($redirectFlag = "Y") {
return 301 https://$host$request_uri;
}
@voidale
voidale commented Jan 10, 2017 edited

Thanks so much helped me a lot!
and the health check oh yeah that's a nice one can't find that over the internet :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment