You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx中使用if指令加载多域名SSL证书报错的解决方法求助

Nginx中使用if指令加载多域名SSL证书报错的解决方法求助

各位大佬好,我最近碰到个Nginx配置的难题,想请教下大家:

我有两个域名 foo.com 和 bar.com,都是用Nginx来提供服务的。为了避免重复写配置代码,我打算把下面这段逻辑加到我的nginx.conf里:

[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检测配置的时候直接报错了,具体错误信息如下:

  • 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

有没有大佬知道我该怎么调整配置才能实现这个需求呀?

备注:内容来源于stack exchange,提问作者somenxavier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 08:33:14