DEV Community

Cover image for Automatic SSL with Let's Encrypt & Nginx

Automatic SSL with Let's Encrypt & Nginx

Adam K Dean on February 13, 2020

See update summary at bottom of post for changelog. Note: December 2020 saw the release of v2 of the letsencrypt-nginx-proxy-companion project. I'...
Collapse
 
ssherlock profile image
ssherlock

To echo everybody else, thank you for a very clear and easy to read article on this subject. I guess there's only one way for me to find out but adding a docker-compose.yml file for the above would have been the icing on the cake (for me at least)

Collapse
 
adamkdean profile image
Adam K Dean

I'm glad you found it clear and easy to read. Apologies for serving cake without icing. I've added a docker-compose setup for this to the bottom of the post. Hope that helps.

Collapse
 
ssherlock profile image
ssherlock

That's brilliant, thanks. And working a treat!

Thread Thread
 
adamkdean profile image
Adam K Dean

Glad to hear it. I've made a further update regarding --expose / VIRTUAL_PORT just in case that section didn't work exactly as expected!

Collapse
 
chen profile image
Chen

Great, simple explanation of these topics. Much appreciated

Is there a simple way to modify the nginx settings for redirection? It seems a bit overkill to run a dedicate container for a redirection rule in the config file

Collapse
 
adamkdean profile image
Adam K Dean

There have been some suggestions to this in recent weeks, yes. You'll have to take a look through the jwilder/nginx-proxy repo issues but IIRC there are a few discussions there. Glad this was helpful though!

I do have plans to extend it, to add things in like IP source restrictions, but just haven't had the time yet.

Collapse
 
olidroide profile image
olidroide

Thanks Adam, good explanation and this was the architecture than I used, but just yesterday I move all my reverse nginx proxy to traefik. But I'm still loving the simplicity of this solution. Thanks for sharing it 😃

Collapse
 
josenunez profile image
Jose Nunez

Hi. Thanks a lot for this post.
I have tried many different ways to write my docker-compose file, and I still can't make this work. I just copied your example and replaced the vhosts with my own. I can always see them working on HTTP. But, on https, chrome returns NET::ERR_CERT_AUTHORITY_INVALID.
I'm using this tool to check geocerts.com/ssl-checker, and it shows the following error:

  • The hostname (canasta-solidaria.org) does NOT match the Common Name in the certificate (letsencrypt-nginx-proxy-companion). This certificate is currently invalid for this host.

I can see that the Common Name is "letsencrypt-nginx-proxy-companion".
I also get exactly the same error for my domain registry.commonsoft.net.
Would you be able to give me a hand with this?
Thanks!

Collapse
 
carmageddon profile image
Genadi Saltikov

I am running into a similar issue, have you ever resolved it, @josenunez ?
The interesting thing is that it works for older subdomains I already had configured, but adding new ones - running into similar one.

Collapse
 
rginus profile image
rginus

Thanks for the explanation and I use your docker-compose.yml file. Maybe you can change the network name to 'service-network' for better understanding.
I have a question and maybe you can help me. I installed Ubuntu with docker on a VM of VMware ESXi with ip address 192.168.1.81. Now I have another VM on the same host with a website running Windows 2016/IIS with ip address 192.168.1.82. Is it possible to access this webserver via the reverse proxy via SSL?

Collapse
 
dineshrathee12 profile image
Dinesh Rathee

LetsEncrypt have revoked around 3 million certs last night due to a bug that they found. Are you impacted by this, Check out ?

DevTo
[+] dev.to/dineshrathee12/letsencrypt-...

GitHub
[+] github.com/dineshrathee12/Let-s-En...

LetsEncryptCommunity
[+] community.letsencrypt.org/t/letsen...

Collapse
 
sammourad profile image
Sam Mourad

Great article. Keep it up!

Collapse
 
hkanizawa profile image
Hudson Kanizawa

Good one. Thanks!

Collapse
 
wellu profile image
Veli pekka Jutila

I have a project in mind where I want to augment my powerdns.com setup to provide also URL redirection and not just DNS. I would give the domain an IP/A-record that points to the nginx-proxy. Then I would use Ansible? to add a new server address to nginx/conf.d/newdomain_com.conf that redirects to what ever the final destination domain is. Maybe even use url-rewrite to redirect single subdomains or URLs.

Letsencrypt would automatically get a certificate for this newdomain, so also https-redirects would work.

Would this setup be feasible to do with the technique you explaned, but without the service containers, since I only need redirects?

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Shame my web host isn't ACME-enabled... alas, switching from them to one that is... is also a pain in the gluteus maximus. Ah well. Such is life sometimes!

Collapse
 
jiazhen profile image
Jiazhen Xie

Great post!

I also found out that you will want PRESERVE_URL: "true" to the adamkdean/redirect image, so that it keeps the url path for CSS and JS.

Collapse
 
prernaweb profile image
Andrew

This is such a clear explanation thanks.

Collapse
 
adamkdean profile image
Adam K Dean

Thanks, glad I was able to get the knowledge across of what could be quite a tricky solution

Collapse
 
orsereiparinha profile image
Sereiparinha OR

Great article. Really appreciated.

btw, is there a simple way to modify Nginx to have basic authentication for a specific virtual host? or container?

Collapse
 
adamkdean profile image
Adam K Dean

Sorry for the very late response. This should be possible yes. See: github.com/nginx-proxy/nginx-proxy...

Collapse
 
mohmehrnia profile image
Mohammad Mehrnia • Edited

How do increase upload file size?