0

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.

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Requiem13. I tried what have you suggested but with no luck. I built a spring boot app that listens on 80th port. Then ELB is set to listen both on 443 and 80 and redirect to 80 of machine running my spring boot app. Do you have any ideas? Maybe I misconfigured something? thx.
Unfortunately this is not useful answer cause this solution leads to infinite loop. And amazon is explaining that in that article I have attached to the initial question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.