B
B
bernex2016-09-23 08:53:14
Nginx
bernex, 2016-09-23 08:53:14

How to generate config for multiple domains?

I want to generate config like this:

server {
        include /etc/nginx/http.conf;
        server_name domain1;
        return 301 https://$host$request_uri;
}
server {
        server_name domain1;
        include /etc/nginx/https.conf;
        ssl_certificate /etc/letsencrypt/live/domain1/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/domain1/privkey.pem;

        auth_basic "Unauthorized";
        auth_basic_user_file /etc/nginx/.htpasswd;

        location / {
                proxy_pass http://dd1;
        }
}

server {
        include /etc/nginx/http.conf;
        server_name domain;
        return 301 https://$host$request_uri;
}
server {
        server_name domain2;
        include /etc/nginx/https.conf;
        ssl_certificate /etc/letsencrypt/live/domain2/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/domain2/privkey.pem;

        auth_basic "Unauthorized";
        auth_basic_user_file /etc/nginx/.htpasswd;

        location / {
                proxy_pass http://dd2;
        }
}

For example (not working):
#!/usr/bin/php-example

<?php

ob_start();

var domains = [
    "domain1": "dd1",
    "domain2": "dd2"
];

foreach(domains as domain=>proxyHost) {
server {
        include /etc/nginx/http.conf;
        server_name $domain;
        return 301 https://$host$request_uri;
}
server {
        server_name $domain;
        include /etc/nginx/https.conf;
        ssl_certificate /etc/letsencrypt/live/$domain/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/$domain/privkey.pem;

        auth_basic "Unauthorized";
        auth_basic_user_file /etc/nginx/.htpasswd;

        location / {
                proxy_pass http://$proxyHost;
        }
}
}

The difficulty is that ssl_certificate does not support variables
I see options to generate bash/php/python
Is there a native or elegant way?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question