Score:0

Using if with ssl_certificate in nginx for loading SSL certificates

jp flag

I have two domains: foo.com and bar.com which are served by nginx. To avoid duplicating code, I want to put this

   [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]

in my nginx.conf but nginx complains:

 * 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

How can I do this?

somenxavier avatar
jp flag
It seems like `ssl_certificate /etc/letsencrypt/live/$host/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/$host/privkey.pem;` works
jp flag
As long as the filename matches the hostname; if you use the same certificate for both domain apex & www subdomain it won't.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.