nginx use $server_name on ssl_certificate path

Solution 1:

You cannot use variables in every directive. ssl_certificate is treated as a literal string and is one of the many directives where variables are unsupported.

To specify different certificates for hosts, you have to explicitly write it in a server block:

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;
    # ...
}
# ...

If you feel uncomfortable duplicating the configuration, create templates and generate the nginx configuration using those templates. See also http://nginx.org/en/docs/faq/variables_in_config.html.

Solution 2:

You can use variables since nginx 1.15.9 (26 Feb 2019)

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

But be aware of Changes with nginx 1.15.12 (16 Apr 2019):

Bugfix: a segmentation fault might occur in a worker process if variables were used in the "ssl_certificate" or "ssl_certificate_key" directives and OCSP stapling was enabled.

Tags:

Nginx