I've been trying to do this for 2 days but to no avail.
In this directory(/etc/nginx/sites-available/sub.example.com) I added this line:
if ($host = www.sub.example.com) {
return 301 https://sub.example.com$request_uri;
}
In this other directory(/etc/nginx/nginx.conf) I added this line:
include /etc/nginx/sites-available/*;
I tested it with (nginx -t) and found no errors! but it does not redirect in any way.