There are many ways to force Nginx to use either WWW version or non-WWW version of URLs for your site.
We use following codes all the time.
Redirect non-www to WWW
server {
server_name example.com;
return 301 http://www.example.com$request_uri;
}
From WWW to non-WWW
server {
server_name www.example.com;
return 301 http://example.com$request_uri;
}
In both cases, for other-www, we create a altogether different server { } block. IMHO, this is cleanest and optimised way to handle www to non-www and non-www to www redirection.
There are some WordPress plugins available there which can handle this at PHP-level. But for performance reason, always handle things in Nginx, that can be handled in Nginx alone!
