[Answer]-Nginx redirects www. to wrong Vhost

1👍

I’m not going to sanitize and validate your configuration files, but they contain several problems and you should DRY them out.

Regarding your actual question.

  1. You haven’t configured any redirects and nginx is therefore happily redirecting those subdomains to your default server.
  2. You haven’t configured a default server and nginx is simply using the very first defined server as default server (in your case site since site2 and site3 come after that one; simple sort).

The actual solution is to configure the redirects you want to happen for each of your servers. This snippet is taken from another answer of mine on a similar question.

server {
    #listen 80 is default
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    #listen 80 is default
    server_name example.com;
    ## here goes the rest of your conf...
}

0👍

you need

server {
    listen       80;
    server_name  mysite1.com;
    return       301 http://www.mysite1.com$request_uri;
}

server {
    listen      80;
    server_name www.mysite1.com;
    # other stuff... 

}

this redirects non-www to www and www to www. you need this config for each of those 3 domains..

Leave a comment