Redirect non www to www SSL for Ghost CMS

I had set up a Ghost blog using their default Ghost CLI. The options I selected were having a www.example.com domain, ssl, and using nginx.

In order to have http://example.com forward to https://www.example.com, I edited the default nginx file.

vi /etc/nginx/sites-enabled/www.example.com.comf

I added example.com to the server-name alongside www.example.com.

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

    server_name www.example.com example.com;
    root /var/www/example.com/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;
        return 301 https://www.example.com$request_uri;
    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}