ELB Nginx redirect http to https

So here is my config:

server {
      listen         80;
      server_name    *.example.com;
      if ($http_x_forwarded_proto = 'http') {            
        return 301 https://$server_name$request_uri;
        }
}

When I go to example.com nginx redirect me to https://example.com but the page is a stub nginx index.html. If I go www.example.com it stays unsecure so it is not redirected at all.

What I am doing wrong? Thanks.

EDIT: When I do like in this article: https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb/

return 301 https://$server_name$request_uri$http_x_forwarded_proto;

Then it is redirected https://example.com/http And of course it is 404 cause http endpoint is stupid.

Upvotes: 0

Views: 317

Answers (1)

C.Vergnaud
C.Vergnaud

Reputation: 875

You really should avoid using "if" in nginx, it is a performance killer.

you should just use this :

server {
  listen      80;
  server_name *.example.com;

  ## redirect http to https ##
  return 301 https://example.com$request_uri;
}

and define your "example.com" server.

if the elb is properly setup to send 443 request to example.com and if you have a listening socket on example.com:443 it will be fine.

Upvotes: 1

Related Questions