I’m running an nginx server on Ubuntu 16.04 on my Digital Ocean droplet to host my multiple domains & subdomains. I was having trouble configuring SSL with Certbot for multiple domains while forwarding WWW subdomains to non-WWW (using this tutorial), so here’s how I did it.
First, have your domains online and accessible via
http. If you are using Ubuntu Server on a Digital Ocean droplet like me, these resources are very helpful.
For this tutorial, we will pretend we have the following domains and we are redirecting each WWW subdomain to the non-WWW version:
sudo add-apt-repository ppa:certbot/certbot
sudo ufw allow 'Nginx Full'
Its a good idea to create a backup of your server before running Certbot.
sudo certbot --nginx -d example.com -d www.example.com -d example.org -d www.example.org
-din front of each domain
- Be sure to run this command with ALL domains/subdomains you want SSL for. I was getting errors and redirect problems when I’d run Certbot for most of my domains and then try and run Certbot for a single domain later on in time.
This is what took me a while. Please note, there is almost definitely a better way to do this, so please let me know (email@example.com), although this is what worked for me. Open the site config file (
sudo nano /etc/nginx/sites-enabled/example.com)
Certbot has mutilated this file (for one, see nginx - If is Evil), but I can’t complain when it is this easy to get SSL certs. Anyways, scroll to the server block where there are 2 if statements. Edit the body of the if statement that starts with
if ($host = www.example.com), to
return 301 https://example.com$request_uri.
Now add the following server block to the bottom of your file
# redirect www -> non-www
Check for config file syntax errors:
sudo nginx -t
sudo systemctl restart nginx