www in domain not working in nginx

my solution and it works for me

server {
    listen 80;
    server_name yourdomainname.com www.yourdomainname.com;
    return 301 https://$server_name$request_uri;
}

write the previous code inside that file --> yourdomainname.conf

nginx


You are rewriting www.domain.com on first few lines of nginx.conf. If i'm not wrong, rewriting and redirecting are different things. Try this on first server block;

server {
    server_name  www.mydomain.com;
    return       301 http://mydomain.com$request_uri;
}

and change

server_name mydomain.com www.mydomain.com

to

server_name mydomain.com

in second server block.