Running node.js app on port 443 (HTTPS)

Dinesh Rawat
2 min readSep 11, 2024

To deploy a Node.js app on EC2 using HTTPS, you need root privileges to bind to port 443. There are two solutions. Shorter Loop usesolution1.

Running node.js app on port 443 (HTTPS)

Solution 1: Grant Node.js the Required Permissions

To let your Node.js app use port 443 without root privileges, use the setcap command to give it the necessary permissions.

sudo setcap 'cap_net_bind_service=+ep' $(readlink -f $(which node))

Solution 2: Use Nginx as a Reverse Proxy

Consider using Nginx as a reverse proxy. It can handle HTTPS, terminate SSL/TLS, and forward requests to your Node.js app on a port that doesn’t require root privileges.

Steps to Set Up Nginx as a Reverse Proxy:

1. Install Nginx:

First, install Nginx on your EC2 instance:

sudo apt-get install nginx

2. Configure Nginx:

Create or modify the Nginx configuration file (usually located in /etc/nginx/sites-available/default) to include the following:

server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /path/to/your/certificate.pem;
ssl_certificate_key /path/to/your/privatekey.pem;
location / {
proxy_pass http://127.0.0.1:3000; # Replace 3000 with your Node.js app's port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
server_name your-domain.com;
return 301 https://$host$request_uri;
}

3. Enable the Site and Restart Nginx:

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
sudo nginx -t # Test the Nginx configuration
sudo systemctl restart nginx

4. Make sure you port 443 is enabled in aws security group.

5. Check the status

After setting up, verify that port 443 is listening:

sudo lsof -i :443
sudo netstat -tuln | grep 443

#SSL #Https #Nodejs #nginx #ReverseProxy

For a detailed guide, check out my blog:

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Dinesh Rawat
Dinesh Rawat

Written by Dinesh Rawat

Seasoned software engineer, Content creator, Helping teams achieve their goals. https://www.linkedin.com/in/dinesh-rawat/

No responses yet

Write a response