This is a way, recommended way by Nginx when creation redirections. i.e. you want to redirect from www to non-ww
server {
server_name www.mydomain.tld;
return 301 $scheme://mydomain.tld$request_uri;
}
server {
server_name mydomain.tld
# ...
}