0

Based on my understadning of the doc, server_name decides which server block will be used.

I have an AWS EC2 instance, and my nginx setup is like the following, where I just simply want to redirect all http requests to https. However, my server_name was mistakenly setup as the following instead of example.com.

server {
  listen         80;
  server_name    ip-xxx-xx-xx-xx.example.com;
  return         301 https://example.com$request_uri;
}

However, when I go to http://example.com, I do get redirect to https://example.com, I wonder why that is happening, shouldn't nginx fail to match my http request to that server block since the server_name is not example.com?

user2002692
  • 971
  • 2
  • 17
  • 34
  • 1
    Because that's how nginx's choose server block. It uses best match for port. If there is only one server block, it will be used. http://nginx.org/en/docs/http/request_processing.html – Alexey Ten Apr 01 '16 at 08:44
  • Good explanation on Digital Ocean https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms#how-nginx-decides-which-server-block-will-handle-a-request – Alexey Ten Apr 01 '16 at 08:50
  • Thank you, that explains everything ! Thanks ! – user2002692 Apr 01 '16 at 08:53

0 Answers0