-1

I have many domains like hello1.com, hello2.com ... hello101.com All of them having SSL Certificates by Let's encrypt. All of this domains need to be proxied to this URI http://127.0.0.1:8000 (django is there and it will handle everything)

So, this is Nginx config for one domain:

server {
    listen  80 default_server;
    return 404;
}

server {
    listen 443 ssl;
    server_name         hello.com;

    location ~ ^/(static|media)/ {
        root            /hosting/hello/files;
        expires         30d;
    }
    location / {
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
         proxy_set_header X-Forwarded-Proto https;
         proxy_pass     http://127.0.0.1:8000;
         proxy_set_header Accept-Encoding  "";
    }

    ssl_certificate /etc/letsencrypt/live/hello.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/hello.com/privkey.pem; # managed by Certbot

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

But if I want to config all my servers, there will be a lot of repeating lines of code and it will be very difficult to maintain.

Only 3 lines of configuration are different for each domain:

server_name        <domain>.com;
ssl_certificate /etc/letsencrypt/live/<domain>.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/<domain>.com/privkey.pem; # managed by Certbot

How can I move all other lines to one place? I don't want to repeat this configuration 100 times.


UPD: I can wildcard server_name or list all domains like this:

server_name   hello1.com hello2.com hello3.com etc.

Then I'll need to put current domain name (is it in $host variable?) into this 2 strings:

ssl_certificate /etc/letsencrypt/live/<domain>/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/<domain>/privkey.pem;

How can I do it?

4
  • Perhaps you can use wildcards in the server_name, otherwise I would look into scripting it.
    – Tommiie
    Jan 30, 2019 at 12:09
  • @Tommiie, how can I wildcard it?
    – Antonio
    Jan 30, 2019 at 12:34
  • Read the documentation: nginx.org/en/docs/http/server_names.html
    – Tommiie
    Jan 30, 2019 at 12:44
  • I can wildcard server_name, but I also need to put domain name here: ssl_certificate /etc/letsencrypt/live/<domain>.com/fullchain.pem; - is it possible?
    – Antonio
    Jan 30, 2019 at 12:49

1 Answer 1

3

If you use different TLS certificates for each name, then you also need to have separate server blocks.

If you have all of the names on the same TLS certificate, then those server names can also share the same server block. To create such a certificate, you would provide multiple -d options to certbot at the same time, e.g. certbot ... -d domain1 -d domain2 -d domain3.

In either case, you can use include yourself to create configuration snippets which you can share among multiple server blocks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .