Syntax
if ($host ~* ^(example\.com|www\.example\.com)$ ){
rewrite ^/(.*)$ https://example.com/$1 permanent;
}
OR better use the following rewrite:
rewrite ^ https://$server_name$request_uri? permanent;
Or use new syntax (recommended):
return 301 https://$server_name$request_uri;