Remove WWW from URLs in nginx

Simply create two server configurations where the first server name redirects to the second one. No regular expressions needed.

server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}

server {
server_name example.com;
# the rest of your configuration
}