Redirect non-www to www URL in Nginx
The best way to redirect non-www to www URL in Nginx according to documentation is to define a separate server for example.org and redirect permanently to www.example.org, here is an example:
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
...
}
Restart and verify config using following commands(for CentOS):
Reload:
systemctl restart nginx
Verify:
systemctl status nginx