I had a Shopify-like application. So, my customer get sub-domain when they create store(i.e customer1.myShopify.com
).
to handle this case of dynamic sub-domains with nginx:
server {
listen 443 ssl;
server_name admin.myapp.com;
ssl_certificate /etc/letsencrypt/live/myapp/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp/privkey.pem;
location / {
proxy_pass http://admin-front-end:80/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
server {
listen 443 ssl;
server_name *.myapp.com;
ssl_certificate /etc/letsencrypt/live/myapp/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp/privkey.pem;
location / {
proxy_pass http://app-front-end:80/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
this works great so if you visit admin.myapp.com
you’ll see the admin application and if you visit any xxx.myapp.com
you’ll see the shop-front-end application.
The Problem
I want to allow my customer to connect their own domain. so I told them to connect with CNAME
and A Record
.
A Record
=> @
=> 12.12.12.3(my root nginx ip)
CNAME
=> WWW
=> thier.myapp.com
not each request to customer.com
will resolved by my nginx.
so I added this configuration to my nginx, to catch all other server_name request:
server {
listen 80;
server_name server_name ~^.*$;
location / {
proxy_pass http://app-front-end:80/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
and it works fine.
but how can I handle SSL
for this case? because it could be any domain name.I don’t know what the customer domain name will be.
how i can give them the ability to add SSL certificate automatically and without create manually ?
4
Answers
This server block should work since variable names are supported for
ssl_certificate
andssl_certificate_key
directives.P.S. Using variable names would compromise performance because now nginx would load the files on each ssl handshake.
Ref: https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_certificate
In my view instead of compromising the nginx performance, we should use one cron job, say letsencrypt bot, which will fetch certificate based on user requested domain, and you can add the certificate in nginx conf, and restart server.
Bonus:
I have used traefik which is kubernetes based solutions, they load configs on the fly without the restart.
In VM you can use the certbot for managing the SSL/TLS certificate.
Now if you are using the
HTTP-01
method to verify your domain you won’t be able to get the Wild card domain name.i would suggest to use the
DNS-01
method in cert-bot for domain verification and you can get the wild card certificate and use it.Adding the certificate to Nginx config using :
If you are using the
certbot
it will also auto inject and add the SSL config above lines to the configuration file.For different domains also you can run the job or certbot with
HTTP-01
method and you will get the certificate.If you are on Kubernetes you can use the cert-manager, which will be managing the
SSL/TLS
certificate.I suggest using separete block with different ssh certificates thats the only solution that worked for me