-1

I'm trying to get the https://domain.ltd redirected to https://www.domain.ltd on Nginx (note the www), here's the config:

server {
    listen 80;
    server_name domain.ltd;
    return 301 https://www.$host$request_uri;
}
server {
    listen 443;
    server_name domain.ltd;
    return 301 https://www.$host$request_uri;
}
server {
    listen 443 ssl;
    server_name www.domain.ltd domain.ltd;
    ...main config goes here
}

What I found strange is that all other options work fine:

http://domain.ltd redirects to https://www.domain.ltd

http://www.domain.ltd redirects to https://www.domain.ltd

https://www.domain.ltd works too

But no way for https://domain.ltd, it only shows ERR_CONNECTION_REFUSED.

What's the problem with configuration? How do I solve it?

UPD. I have both www.domain.ltd and domain.ltd included in my SSL cert, so there's no reason not to work

  • if included in your SSL cert, why is ssl it not in your config – Colt Jul 23 '17 at 18:15
2

You have to turn on ssl.

SSL is a protocol, HTTP is an another protocol. At the server side, port number won't implicitly specify protocols. You can run either one at any ports. At the other hand, browsers implicitly try to communicate using http at port 80 and https (ssl) at 443.

Naturally, if the two party speaks different language, communication error occurs.

It doesn't matter if you just want to redirect something, opening a websocket or get a web-page, you must comply the browser expectations OR specify explicitly what you want.

You can say http://some.site.com:443/ to force the client (browser) to use the non implicit protocol (http without 's' on 443) which will most possibly work with your current config. But the right thing to do here is not to confuse your users and always include ssl (and all bells and whistles) whenever using port 443.

1

You've made a number of error

  • Not listening on SSL
  • Not specifying SSL keys and certificates
  • Specifying a domain in two server blocks
  • Missing slashes

If you don't know about SSL try reading my Nginx/SSL tutorial, which will help you get a free Let's Encrypt SSL certificate.

Here's a config to try

server {
  listen 80;
  server_name domain.ltd;
  return 301 https://www.domain.ltd/$request_uri;
}
server {
  listen 443 ssl;
  server_name domain.ltd;
  ssl_certificate ....; # Insert certificate references
  ssl_certificate_key ...; # Insert certificate key references
  return 301 https://www.domain.ltd/$request_uri;
}
server {
  listen 443 ssl;
  server_name www.domain.ltd;
  ssl_certificate ....; # Insert certificate references
  ssl_certificate_key ...; # Insert certificate key references
  ...main config goes here
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.