Actually the code to add to nginx config:

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

as a bonus: redirect from www.* to just second level domain:

server {
    listen 443 ssl http2;
    server_name www.example.com;

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