nginx dynamic HTTP/S resolves to https://_

I'm trying to let all traffic for my nginx be redirected to HTTPS, independent of server name. So, any other vhost should be redirected to its HTTPS counterpart.

example.com -> https://example.com
test.com -> https://test.com
...

Yet, instead of using the incoming $server_name or $host (tried both), it keeps redirecting to a plain https://_. Is my config incorrect?

server {
  listen        80 default_server;
  listen        [::]:80 default_server;
  server_name   _;
  return        301 https://$server_name$request_uri;
}

1 answer

  • answered 2018-01-10 08:54 nilo

    Okay, rather silly. If you have previously setup any non-working configuration (e.g. one which will result in the faulty redirect), the browser will remember this redirect without asking the server again (as according to HTTP 301 - permanently moved). So, either clear the browser's data or try with a different one.