【问题标题】:how to fix nginx multiple domains SSL error如何修复 nginx 多域 SSL 错误
【发布时间】:2019-07-15 19:20:44
【问题描述】:

我有 2 个域,我的 nginx 配置如下。当我输入 example.com 时,网站使用 ssl 打开没有任何问题,但如果我输入 example.nl 浏览器会给出“您的连接不是私有的”警告。

我通过更改配置测试了两个域证书,并且两者都单独工作没有问题。

有什么问题?我该如何解决?

upstream website {
    server web:8000;
}

server {
    listen 80;
    server_tokens off;
    server_name *.example.com *.example.nl;


    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name *.example.com;
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;


    # serve static files
    location /static/ {
        alias /static/;
    }

    # serve media files
    location /media/ {
        alias /media/;
    }


    location / {
        proxy_pass  http://ourwebsite;
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    }
}


server {
    listen 443 ssl;
    server_name *.example.nl;
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/example.nl/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.nl/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;


    # serve static files
    location /static/ {
        alias /static/;
    }

    # serve media files
    location /media/ {
        alias /media/;
    }


    location / {
        proxy_pass  http://website;
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    }
}

【问题讨论】:

  • 您是否尝试过添加为可用站点中的另一个站点,而不是同一站点中的另一个服务器?
  • 查看此链接作为示例。我认为 nginx 将使用最后定义的任何内容stackoverflow.com/questions/33055212/…
  • 我在 docker contains 中运行 nginx,我在 etc 中检查了 sites-available 文件夹,它不存在。
  • /etc/nginx/sites-available/
  • 是的,我检查了同一个目录。我想我应该能够用单个配置文件解决这个问题。他们说分离配置文件只是上述问题中的一种约定。

标签: ssl nginx devops lets-encrypt nginx-config


【解决方案1】:

TLDR:你的名字不匹配

您有用于 SSL 的 server{} 块,具有 server_name*.example.com*.example.nl,但您正在请求 example.comexample.nl。这些不匹配任何一个服务器块。像 *.example.com 这样的 DNS 通配符匹配该标签中的 any 值(abc.example.comwazoo.example.comoompapaoompapaoompapa.example.com),但它与带有 no 的名称不匹配标签如example.com。您可能会将它与 shell 'glob' 或正则表达式(例如 sed、awk、perl 等)混淆,其中* 是具有零个或多个语义的'Kleene star'。

由于您的请求与任何一个服务器块都不匹配,nginx 使用默认的服务器块;由于您没有明确指定默认块,因此 nginx 将其设为第一个。第一个 SSL 服务器块使用 example.com 的证书,因此对 example.com 的请求与该证书匹配并且有效,而对 example.nl 的请求不匹配并失败。如果您配置*.example.nl 块,它现在是第一个,因此也是默认值,因此对example.nl 的请求工作但对example.com 的请求失败。

将您的 server_name 更改为或包含您使用的实际名称的匹配项。

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 2018-03-06
    • 2019-08-01
    • 2018-11-25
    • 2022-06-13
    • 2018-01-15
    • 2017-12-11
    • 2017-01-12
    相关资源
    最近更新 更多