Add www to non-www address with ssl

Solution 1:

This is a stripped down version of my own config, which I think does what you want:

server {
    listen 80  default;
    listen 443 default ssl;
    server_name *.example.com;

    if ($host = example.com) {
        rewrite ^(.*) $scheme://www.example.com$1 permanent;
    }

    root   /opt/example;

    ssl                   on;
    ssl_certificate       /opt/nginx/ssl/server.crt;
    ssl_certificate_key   /opt/nginx/ssl/server.key;
}