In short, you need to add a new server block outside of the already excisting server block that is written in /etc/nginx/conf.d/default.conf (pay attention though, as this file could be named something different as well)
In this case I already had code required to run xenforo. The snippet starting with server { at the top is the one you need to add at the top or at the end of the file, it should redirect all non-www requests to a www equivalent by redirecting it through nginx.
Code:
server {
listen 80;
server_name yourdomainname.com;
return 301 www.yourdomainname.com$request_uri;
}
server {
listen 80;
server_name www.yourdomainname.com;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$uri&$args;
}
location /internal_data/ {
internal;
}
location /library/ {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}