Skip to content

Instantly share code, notes, and snippets.

@waiphyo285
Created July 10, 2024 17:04
Show Gist options
  • Save waiphyo285/4ab1edc9adfc30069475851ab096d88e to your computer and use it in GitHub Desktop.
Save waiphyo285/4ab1edc9adfc30069475851ab096d88e to your computer and use it in GitHub Desktop.

Setting Up Nginx with SSL on Ubuntu for Domain Proxy

This guide will walk you through the steps to install Nginx on an Ubuntu server, set up a domain to proxy to localhost:3000, and install an SSL certificate using Certbot.

Step 1: Update Your Package Index

First, ensure that your package index is up to date:

sudo apt update

Step 2: Install Nginx

Install the Nginx web server:

sudo apt install nginx

Step 3: Start and Enable Nginx

Start the Nginx service and enable it to start on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 4: Configure Nginx to Proxy Requests

Create a New Configuration File Create a new configuration file for your domain:

sudo nano /etc/nginx/sites-available/yourdomain.com

Add the Following Configuration Add the following content to the file:

server {
    listen 80;
    server_name hellomm.com www.yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        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 Configuration

Create a symbolic link to enable the configuration:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

Step 5: Test Nginx Configuration

Test the Nginx configuration for syntax errors:

sudo nginx -t

If the test is successful, you will see:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 6: Restart Nginx

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 7: Update DNS Records

Ensure that your domain www.yourdomain.com points to the IP address of your Ubuntu server through your domain registrar's control panel.

Step 8: Ensure Firewall Rules Allow HTTP and HTTPS Traffic

If you are using UFW (Uncomplicated Firewall), allow HTTP and HTTPS traffic:

sudo ufw allow 'Nginx Full'
sudo ufw reload

Step 9: Install Certbot and the Nginx Plugin

Install Certbot and the Nginx plugin:

sudo apt install certbot python3-certbot-nginx

Step 10: Obtain and Install the SSL Certificate

Run the following command to obtain and install the SSL certificate:

sudo certbot --nginx --non-interactive --keep-until-expiring --renew-with-new-domains --agree-tos --email youremail@gmail.com --no-eff-email --domains yourdomin.com,www.yourdomain.com

Step 11: Verify Certbot Auto-Renewal

Certbot sets up a cron job or systemd timer to renew the certificate automatically before it expires. Test the auto-renewal process:

sudo certbot renew --dry-run

If the test is successful, Certbot will renew the certificate automatically as needed.

Final Verification

After following these steps, your domain www.yourdomain.com should be securely proxied to localhost:3000 with an SSL certificate installed and auto-renewal configured.

By following this guide, you have successfully set up Nginx on your Ubuntu server, configured it to proxy requests to localhost:3000, and secured your domain with an SSL certificate using Certbot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment