Nginx redirecting all domains to www subdomain
Solution 1:
In your config first server
block is the default_server which will be used for all requests that don't match a more specific server.
Rather than return 301 https://www.$host$request_uri;
in the default_server don't use the Host header from the request as a parameter to generate the redirect, as that can become all kinds of incorrect things, instead use return 301 https://www.example.com$request_uri;
to always redirect to www.example.com