在 nginx 中使用 if 和 ssl_certificate 加载 SSL 证书

在 nginx 中使用 if 和 ssl_certificate 加载 SSL 证书

我有两个域名:foo.com和,bar.com它们由 nginx 提供服务。为了避免重复代码,我想把这个

   [some code]

        if ($host = "foo.com") {
           ssl_certificate /etc/letsencrypt/live/foo.com/fullchain.pem;
           ssl_certificate_key /etc/letsencrypt/live/foo.com/privkey.pem;
        }

        if ($host = "bar.com") {
          ssl_certificate /etc/letsencrypt/live/bar.com/fullchain.pem;
          ssl_certificate_key /etc/letsencrypt/live/bar.com/privkey.pem;
        }

   [some code there]

在我的 nginx.conf 中但 nginx 抱怨:

 * Checking nginx' configuration ...
2023/07/18 20:10:15 [emerg] 2656#2656: "ssl_certificate" directive is not allowed here in /etc/nginx/nginx.conf:46
nginx: configuration file /etc/nginx/nginx.conf test failed
2023/07/18 20:10:15 [emerg] 2657#2657: "ssl_certificate" directive is not allowed here in /etc/nginx/nginx.conf:46
nginx: configuration file /etc/nginx/nginx.conf test failed

我怎样才能做到这一点?

相关内容