How to Set Up Nginx as a Reverse Proxy with Free SSL (Let’s Encrypt)

Harshit PaneriHarshit Paneri
3 min read

If you’re looking to host a website or application and want to secure it with HTTPS, this guide is for you. We’ll walk you through setting up Nginx as a reverse proxy and installing a free SSL certificate using Certbot (Let’s Encrypt). This guide is written in simple terms, so even if you’re a beginner, you can follow along!


Step 1: Install Nginx

Nginx is a popular web server that can also act as a reverse proxy to forward requests to other servers.

  1. Install Nginx in you machine
    Open your terminal and run the following command:

     sudo apt install nginx -y
    
  2. Start Nginx and Enable it to Start on Boot
    Run these commands to start Nginx and make sure it automatically starts every time your server boots:

     sudo systemctl start nginx
     sudo systemctl enable nginx
    
  3. Check if Nginx is Running
    Use this command to verify that Nginx is up and running:

     sudo systemctl status nginx
    

    You should see a message indicating Nginx is active (running).


Step 2: Install Certbot (Let’s Encrypt Client)

Certbot is a tool that helps you generate and manage free SSL certificates from Let’s Encrypt.

  1. Install Certbot and the Nginx Plugin
    Run the following command:

     sudo apt install certbot python3-certbot-nginx -y
    

Step 3: Configure Nginx as a Reverse Proxy

Now that Nginx is installed, let’s configure it as a reverse proxy.

  1. Create a New Nginx Configuration File
    Replace <your-domain> with your domain name (e.g., xyz.online):

     sudo nano /etc/nginx/sites-available/<your-domain.conf>
    

    Add the following configuration inside the file:

     server {
         listen 80;
         server_name <your-domain> #backend.xyx.com;
    
         location / {
             proxy_pass http://localhost:5000; # Change this to the port your app is running on
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
     }
    
  2. Save the File and Exit
    Press CTRL+O to save and CTRL+X to exit.

  3. Enable the Configuration
    Create a symbolic link to enable your configuration:

     sudo ln -s /etc/nginx/sites-available/<your-domain.conf> /etc/nginx/sites-enabled/
    
  4. Test the Nginx Configuration
    Run this command to ensure there are no errors:

     sudo nginx -t
    

    If everything is okay, you’ll see:

     nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
     nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  5. Reload Nginx
    Apply your changes by reloading Nginx:

     sudo systemctl reload nginx
    

Step 4: Allow Traffic Through the Firewall

  1. Allow Nginx Full Traffic
    Update your firewall to allow Nginx traffic:

     sudo ufw allow 'Nginx Full'
    

Step 5: Obtain a Free SSL Certificate with Let’s Encrypt

  1. Run Certbot
    Use Certbot to automatically configure SSL for your domain:

     sudo certbot --nginx -d <your-domain>
    

    Replace <your-domain> with your actual domain name (e.g., xyz.online).

  2. Follow the Prompts
    Certbot will guide you through a few steps. Once it’s done, your SSL certificate will be installed and configured!


Step 6: Verify Everything is Working

  1. Test Your Website
    Open your browser and go to https://<your-domain>. You should see your site running securely with HTTPS.

  2. Automatic Renewal
    Let’s Encrypt certificates expire in 90 days, but Certbot sets up automatic renewal for you. To test it, run:

     sudo certbot renew --dry-run
    

Step 7: Update Your DNS Records (Optional)

Make sure your domain points to your server’s IP address by updating the DNS records. For example:

  • Add an A record for your domain (backend.xy.online) pointing to your server’s public IP.

Final Thoughts

Congratulations! 🎉 You’ve successfully set up Nginx as a reverse proxy with Let’s Encrypt SSL. Your website or application is now secure and accessible over HTTPS.

If you encounter any issues, feel free to drop a comment below, and I’ll be happy to help. Happy hosting!

4
Subscribe to my newsletter

Read articles from Harshit Paneri directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshit Paneri
Harshit Paneri

🚀 Hi, I'm Harshit, a versatile Full Stack Developer with a passion for Software Development and DevOps. With a background in Computer Science Engineering, I've honed my skills to not just build robust APIs but also ensure they run seamlessly in production. From coding the backend to setting up CI/CD pipelines, I manage the entire lifecycle of applications—developing, deploying, and scaling them as needed. Whether it's integrating cutting-edge technologies, optimizing performance, or setting up monitoring and alerting for smooth operations, I make sure everything runs like a well-oiled machine. In short, I’m the go-to person for creating solutions that are reliable, scalable, and production-ready.