大网数据广告活动
服务器运维问答

https with sni two certs tho server names but same config. can I use one server block?

· 52CE 我爱测

I often encounter following problem. I have an nginx server serving two hostnames via https on the same IP and the same port. Each host name has it's own cert. What I am doing so far is to have two configurations: server { listen…

问题原文

原帖链接 · gelonida · CC BY-SA 4.0

I often encounter following problem.

I have an nginx server serving two hostnames via https on the same IP and the same port. Each host name has it's own cert.

What I am doing so far is to have two configurations:

server {
    listen              443 ssl;
    server_name         www.example1.com;
    ssl_certificate     www.example1.com.crt;
    ssl_certificate_key www.example1.com.key;
    ssl_protocols       ...;
    ssl_ciphers         ...;
    ...
}

and

server {
    listen              443 ssl;
    server_name         www.example2.com;
    ssl_certificate     www.example2.com.crt;
    ssl_certificate_key www.example2.com.key;
    ssl_protocols       ...;
    ssl_ciphers         ...;
    ...
}

Is there any trick to have this done in only one server block?

The reason I'm asking is, that both servers share apart of the name and the cert exactly the same config.

What I do so far is:

server {
    listen              443 ssl;
    server_name         www.example1.com;
    ssl_certificate     www.example1.com.crt;
    ssl_certificate_key www.example1.com.key;
    include /etc/nginx/common_config/example1_and_2/*;
}

server {
    listen              443 ssl;
    server_name         www.example2.com;
    ssl_certificate     www.example2.com.crt;
    ssl_certificate_key www.example2.com.key;
    include /etc/nginx/common_config/example1_and_2/*;
}

Can this be improved is there some standard recommendation? If this is at good as it gets, is there at least a recommendation for the path to such common config giles?

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 4.0

The server_name can have a list of multiple hostnames, e.g.

server_name example.org www.example.org example.com www.example.com;

While (since 1.11.0) it has been possible to load multiple certificate in a single server { } block, it's for multiple different types (e. g.RSA and ECDSA), not for multiple hostnames. But it's possible to use variables in ssl_certificate:

Since version 1.15.9, variables can be used in the file name when using OpenSSL 1.0.2 or higher:

ssl_certificate $ssl_server_name.crt;
ssl_certificate_key $ssl_server_name.key; 

Note that using variables implies that a certificate will be loaded for each SSL handshake, and this may have a negative impact on performance.

For the performance impact mentioned I wouldn't recommend that, but would combine all the hostnames for the same server { } block into a single certificate as Subject Alternative Names (SAN), instead.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-04-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。