Skip to content

Instantly share code, notes, and snippets.

@waiphyo285
Last active October 20, 2024 08:10
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.

Setup Nginx with SSL for Domain Proxy on Ubuntu

This guide explains how to install Nginx on Ubuntu, configure it to proxy a domain to localhost:3000, and set up SSL using Certbot for HTTPS encryption.

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

Point your domain (www.yourdomain.com) to your server's IP by setting an A record in 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 [email protected] --no-eff-email --domains yourdomin.com,www.yourdomain.com

Step 11: Verify Certbot Auto-Renewal

Certbot automatically sets up renewal via cron or systemd. To test certificate auto-renewal, run:

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