← All Insights

How to Set Up Let's Encrypt SSL Certificate with Nginx

HTTPS is no longer optional. Browsers flag HTTP sites as insecure, search engines penalize them, and users are increasingly aware of the lock icon in the address bar. Let's Encrypt provides free, automated SSL/TLS certificates, and Certbot makes the setup straightforward.

This guide walks through the complete process of configuring HTTPS on an Nginx server with automatic renewal.


Prerequisites

Before starting, ensure you have:

  • A Linux server running Ubuntu
  • Nginx installed and running
  • A registered domain name
  • An A record pointing your domain to the server's IP address
  • Ports 80 and 443 open in your firewall

DNS propagation can take up to 48 hours. Verify your DNS record is resolving correctly before proceeding:

dig example.com A

Step 1: Install Certbot

Install Certbot and the Nginx plugin:

sudo apt update
sudo apt install certbot python3-certbot-nginx

Step 2: Configure Nginx for Your Domain

Create an Nginx server block configuration for your domain. Create a file at /etc/nginx/sites-available/example.com:

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the site and test the configuration:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 3: Obtain the SSL Certificate

Run Certbot with the Nginx plugin. It will automatically detect your server block, obtain the certificate, and modify your Nginx configuration to enable HTTPS:

sudo certbot --nginx -d example.com -d www.example.com

Certbot will prompt you for an email address for renewal notifications and ask whether to redirect HTTP traffic to HTTPS. Choose the redirect option, it ensures all traffic uses HTTPS automatically.


Step 4: Verify the Configuration

After Certbot completes, verify the certificate is working:

sudo nginx -t
sudo systemctl reload nginx

Check the certificate details:

openssl s_client -connect example.com:443 -servername example.com < /dev/null 2>/dev/null | \
  openssl x509 -noout -dates

This shows the certificate's validity period and confirms it was issued correctly.


Step 5: Verify Automatic Renewal

Let's Encrypt certificates expire after 90 days. Certbot installs a systemd timer or cron job to renew certificates automatically before expiry. Test that the renewal process works:

sudo certbot renew --dry-run

A successful dry run confirms that automatic renewal is configured correctly. You should not need to manually renew certificates, Certbot handles this.


Troubleshooting Common Issues

DNS not resolving to the correct IP: The most common cause of certificate issuance failure. Let's Encrypt must be able to reach your server via HTTP on port 80 to complete the domain validation challenge. Verify your DNS A record and ensure no proxies or CDNs are interfering.

Port 80 blocked by firewall: Certbot's HTTP challenge requires port 80 to be accessible from the internet. Check your UFW or iptables rules:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Nginx configuration syntax error: Always run sudo nginx -t before reloading. A syntax error will prevent Nginx from reloading and may take your site offline.

Redirect loop: If your application is behind a reverse proxy that adds its own redirects, you may create a loop. Ensure X-Forwarded-Proto headers are set correctly and your application trusts the proxy.

Renewal failures: Check the Certbot log at /var/log/letsencrypt/letsencrypt.log for details. Common causes are DNS changes, firewall changes, or the application server not running during the renewal HTTP challenge.


Nginx Configuration After Certbot

After running Certbot, your Nginx configuration will be updated to include SSL settings. The result should look similar to this:

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

Conclusion

HTTPS should be treated as part of your application's infrastructure, not simply a certificate that you install once and forget about. Let's Encrypt and Certbot have removed the cost and most of the complexity from this process. There is no longer a reason to deploy any web application without HTTPS.

Verify your renewal is working. Monitor your certificate expiry dates. And treat any HTTP traffic to your production services as a configuration issue to fix, not an acceptable state.