nginx redirecting to wrong vhost when both hosts use ssl

Viewed 1028

I have 2 virtual hosts configured in nginx and both using ssl in a way that http://www.firstsite.com redirects to https://www.firstsite.com and it works correctly, the problem is that http://www.secondsite.com is not redirecting to https://www.secondsite.com, but to https://www.firstsite.com

this is the first config file

server {
     listen 80; 
     return 301 https://www.dianadelvalle.com$request_uri;
     server_name www.dianadelvalle.com;

  }
  server{
      listen 443 ssl;
      ssl_certificate     /etc/letsencrypt/live/www.koohack.com/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/www.koohack.com/privkey.pem;

      root /home/pi/www.dianadelvalle.com/;

    index commingsoon.html index.html index.htm index.nginx-debian.html;

      server_name www.dianadelvalle.com;
      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;

      # max upload size
      client_max_body_size 5M;   # adjust to taste

      location / {
        try_files $uri $uri/ =404;
    }
  }

and the second config file:

# the upstream component nginx needs to connect to
upstream django {
    server unix:///home/pi/koohack/mysite.sock; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

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

# configuration of the server
server {
    listen 443  ssl;
    server_name www.koohack.com;

    ssl_certificate /etc/letsencrypt/live/www.koohack.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.koohack.com/privkey.pem;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # max upload size
    client_max_body_size 15M;   # adjust to taste

    if (-f /home/pi/koohack/.maintenance) {
         return 503;
    }

   error_page 503 @maintenance;
   location @maintenance {
        rewrite ^(.*)$ /home/pi/koohack/static/maintenance.html break;
   }

    # Django media
    location /media  {
        alias /home/pi/koohack/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/pi/koohack/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
    location /.well-known {
        alias /home/pi/koohack/.well-known;
    }
}

I spared the server name, log and certificate paths for clarity. What I'm doing wrong? Any suggestions?

Necessary note: I already looked to this possible answer to avoid content duplication but it didn't help

1 Answers
Related