当两个主机都使用ssl时,nginx重定向到错误的vhost

时间:2017-10-06 04:06:32

标签: ssl nginx lets-encrypt

我在nginx中配置了2个虚拟主机,并且都以http://www.firstsite.com重定向到https://www.firstsite.com的方式使用ssl并且它正常工作,问题是http://www.secondsite.com没有重定向到{ {3}},但https://www.secondsite.com

这是第一个配置文件

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;
    }
  }

和第二个配置文件:

# 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;
    }
}

为了清楚起见,我省去了服务器名称,日志和证书路径。我做错了什么?有什么建议吗?

必要注意:我已经查看了https://www.firstsite.com以避免内容重复,但它没有帮助

1 个答案:

答案 0 :(得分:0)

您可能有以下配置:

server_name my.domain.com;
ssl_certificate     /etc/nginx/chain.pem;
ssl_certificate_key /etc/nginx/my.domain.key;

检查您的第二个站点是否也在侦听ssl端口。

 listen 443 ssl;
 listen [::]:443 ssl;

如果第二个站点缺少侦听配置,它将重定向到默认值,无论ssl证书配置如何。