Nginx too many redirect when trying to redirect non www to www

Solution 1:

You need to have separate server blocks for your www and non-www domains.

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate ...
    ssl_certificate_key ...
    server_name example.it;

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

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate ...
    ssl_certificate_key ...
    server_name www.example.it;

    ... rest of configuration ...
}