nginx 在 ssl_certificate 路径上使用 $server_name

Cla*_*nho 36 nginx

如何在文件路径中使用变量名?

ssl_certificate /home/ec2-user/.certificados/$server_name.crt;
ssl_certificate_key /home/ec2-user/.certificados/$server_name.key;
Run Code Online (Sandbox Code Playgroud)

Lek*_*eyn 41

您不能在每个指令中使用变量。ssl_certificate被视为文字字符串,并且是许多不支持变量的指令之一。

要为主机指定不同的证书,您必须将其明确写入服务器块中:

server {
    server_name example.com;
    ssl_certificate /home/ec2-user/.certificados/example.com.crt;
    ssl_certificate_key /home/ec2-user/.certificados/example.com.key;
    # ...
}
server {
    server_name example.net;
    ssl_certificate /home/ec2-user/.certificados/example.net.crt;
    ssl_certificate_key /home/ec2-user/.certificados/example.net.key;
    # ...
}
# ...
Run Code Online (Sandbox Code Playgroud)

如果您对复制配置感到不舒服,请创建模板并使用这些模板生成 nginx 配置。另请参阅http://nginx.org/en/docs/faq/variables_in_config.html

  • 今天添加了对 `ssl_certificate` 和 `ssl_certificate_key` 中变量的支持!http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_certificate (9认同)

小智 12

您可以使用自nginx 1.15.9(2019 年 2 月 26 日)以来的变量

请注意,使用变量意味着将为每次 SSL 握手加载证书,这可能会对性能产生负面影响

不过,要注意与变化nginx的12年1月15日(2019年4月16日):

修正:如果在“ssl_certificate”或“ssl_certificate_key”指令中使用了变量并且启用了 OCSP 装订,则可能会在工作进程中发生分段错误。

  • 这里要记住的是,当证书具有静态路径时,它会在以 root 身份运行的初始化时加载。使用变量时,它在运行时加载,通常作为 www 或 nginx 运行,因此它可能没有权限。 (2认同)