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

标签 ssl nginx lets-encrypt

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

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

必要的说明:我已经看过this possible answer避免内容重复,但没有帮助

最佳答案

您可能有以下配置:

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 证书配置如何。

关于ssl - 当两个主机都使用 ssl 时,nginx 重定向到错误的 vhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46598100/

相关文章:

apache - 在 Ubuntu 12.04 上使用 Nginx SSL 终止的 Varnish 3.0 [无限循环问题]

ssl - Kubernetes/Ingress Nginx/Cert Manager 证书有命名空间?

python-3.x - 带有 gaiohttp 工作线程的 Gunicorn 总是使用 Flask 应用程序返回 404

ssl - 自动更新 website.com 和 *.website.com 证书的方式

amazon-web-services - 一个 EC2 实例上的多个 SSL 证书

c - SSL CA 证书 - LibCurl C 语言 (Linux)

c# - 跨线程使用不同安全协议(protocol)的 .NET https 请求

php - 如何使用 curl 解码 "Content-Encoding: gzip, gzip"?

asp.net - NGINX HTTPS 代理无法使用 Azure AD 重定向 Uri

ssl - 无法在 cert-manager 中获取 TLS 证书