Nginx ssl_certificate 基于 $host?

Emm*_*-Ab 5 ssl nginx ssl-certificate

您好,我该如何建立基础ssl_certificatessl_certificate_key基于$host这样我就能够做到:

if $host = domain.com than set one path, if $host = example.com than set another path

我在用nginx/1.15.8

如果我为域创建不同的块,我会在重新加载时收到错误:

nginx: [emerg] duplicate zone "cache"因此我需要 1 个块并根据域加载证书。

Iva*_*sky 5

您可以使用map指令替换:

map $host $certfile {
    domain.com    /path/to/certificate_1;
    example.com   /path/to/certificate_2;
}
map $host $keyfile {
    domain.com    /path/to/key_1;
    example.com   /path/to/key_2;
}

server {
    ...
    ssl_certificate        $certfile;
    ssl_certificate_key    $keyfile;
    ...
}
Run Code Online (Sandbox Code Playgroud)

更新1

正如 @AlexeyTen 指出的,此配置需要 nginx >= 1.15.9,因此它不适用于 nginx 1.15.8(或最新的 openresty 生产版本 1.15.8.3)。如果您使用的是 openresty,使用此配置的唯一方法是手动构建和安装 1.17.8.1 RC1。

更新2

作为替代方案,您可以请求一个对两个域均有效的新证书。我使用以下命令通过 bash 脚本自己完成了此操作:

acme.sh --issue -d domain.com -d example.com --webroot /my/web/root
Run Code Online (Sandbox Code Playgroud)