The trick is to make a server block that matches the www
url's and redirect them to a non-www
url, add this above your server block after replacing example.com
with your actual domain.
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri$is_args$query_string;
}
server {
server_name example.com;
# your actual config goes here
}