37
votes

I'm setting up an nginx server with an SSL.

The domain with the ssl is www.mydomain.com

I want to redirect all requests from:

http://mydomain.com, http://www.mydomain.com, & https://mydomain.com to

https://www.mydomain.com

I have the following server blocks setup currently:

server{
  listen 443 ssl;
  root /www/mydomain.com/;

  ssl_certificate /ssl/domain.crt;
  ssl_certificate /ssl/domain.key;
  .
  . 
  .
}

server{
  listen 80;
  server_name mydomain.com;
  return 301 https://www.mydomain.com$request_uri;
}

server{
  listen 80;
  server_name www.mydomain.com;
  return 301 https://www.mydomain.com$request_uri;
}

server{
  listen ssl 443;
  server_name mydomain.com;
  return 301 https://www.mydomain.com$request_uri;
}

This currently does not work, but I don't understand why not. I can get a combination of either http -> https working or no-www to -> www working, but mixing them as above does not work.

6

6 Answers

18
votes

The ssl redirect won't work if your ssl certificate doesn't support the non-www domain. The config is correct but can be reduced to just 1 redirect server

Also don't forget to reload nginx sudo service nginx reload

server {
  listen 80;
  listen 443 ssl;
  server_name example.com;
  # add ssl settings
  return 301 https://www.example.com$request_uri;
}
13
votes

I am late, But you can do like this

server{
  listen 443 ssl;
  server_name www.mydomain.com;
  root /www/mydomain.com/;

  ssl    on;
  ssl_certificate /ssl/domain.crt;
  ssl_certificate /ssl/domain.key;
  .
  . 
  .
}

server{
  listen 80;
  server_name www.mydomain.com mydomain.com;
  return 301 https://$server_name$request_uri;
}

server{
  listen 443;
  server_name mydomain.com;
  return 301 https://www.$server_name$request_uri;
}

Or you can replace return 301 https://www.$server_name$request_uri; with rewrite ^ http://www.$server_name$request_uri? permanent;, both will work.

You also need to set this in google webmaster for better SEO.

1
votes

this works for me for http to https redirection,

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name example.com;

    #For HTTP to HTTPS:

    proxy_set_header X-Forwarded-Proto $scheme;
    if ( $http_x_forwarded_proto != 'https' ) 
    {
        return 301 https://$host$request_uri;
    }

    location / {
        try_files $uri $uri/ /index.php;
        add_header 'Access-Control-Allow-Origin' '*';
    }
    
    location ~ \.php$ {
        include fastcgi.conf;   
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    
    location ~ /\.ht {
        deny all;
    }
}

thanks.

0
votes
#If you like to redirect all "http" to "https" then add the following:
server {
        listen 80;

        server_name yourdomain.com;
        server_name www.yourdomain.com;

 if ($scheme = "http")
        {
                rewrite ^(.*)$ https://yourdomain.com$1 permanent;
        }
}
0
votes

Use a rewrite to send all HTTP traffic to HTTPS:

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

This configuration listens on port 80 as the default server for both IPv4 and IPv6 and for any hostname. The return statement returns a 301 permanent redirect to the HTTPS server at the same host and request URI.

0
votes
Please add two given things on your file.

Given code paste on top side 
server {
    listen 80;
    server_name linuxize.com www.linuxize.com;
    return 301 https://linuxize.com$request_uri;
}

Given code paste on bottom side.
server {
    listen 443 ssl http2;
    server_name www.linuxize.com;

    # . . . other code

    return 301 https://linuxize.com$request_uri;
}

For more click here!