Search code examples
amazon-web-servicesnginxamazon-elastic-beanstalk

Redirect Elastic Beanstalk HTTP requests to HTTPS with nginx


I want a redirect from HTTP request to HTTPS on Elastic Beanstalk with nginx as proxy system.

I've found a lot of advices on Google but no one helped, it doesn't redirect.

That is my current test.config file in .ebexentions directory:

files:
"/etc/nginx/conf.d/proxy.conf" :
    mode: "000644"
    owner: root
    group: root
    content: |
        server{
            if ($http_x_forwarded_proto = "http") {
                return 301 https://$host$request_uri;
            }
        }

I've also tried countless other settings, none of them worked.

That are my load balancer settings: enter image description here

I hope you can help me. :)


Solution

  • This is the only solution that worked.

    It's necessary to overwrite the default nginx file after AWS created it. So there has to be two more files:

    1. Write the nginx file.
    2. Create a script that overwrites the default nginx file.
    3. Run the script after AWS created the default file.