Domain redirects to wrong site in nginx, multiple sites in config files

Solution 1:

If there is no default site config then nginx will auto select the first available config file when presented with a request it doesn't have specifically configured.

In this case when you add the www in front of either domain nginx is selecting website1.com config

To resolve this particular issue replace the following lines of the configs

website1.com config

  server_name website1.com www.website1.com;

website2.com config

  server_name website2.com www.website2.com;

You can alternatively use *.website1.com or *.website2.com to capture all requests.

You should really have a default site config though as there are a few quirks like this that can be avoided by having one. Even if it simply does a 400 error return.