How to Fix Node.js Server Not Accessible from External IP on Ubuntu: Port 8080 Troubleshooting Guide


2 views

When running a Node.js server on Ubuntu, you might encounter a situation where:

netstat -pan | grep 80
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN          16785/node

Shows your Node.js process is only binding to 127.0.0.1 (localhost) instead of all available interfaces (0.0.0.0).

The fundamental issue lies in how your Node.js application binds to network interfaces. Here's how to fix it in your server code:

// Correct way (binds to all interfaces)
const server = app.listen(8080, '0.0.0.0', () => {
    console.log(Server running at http://0.0.0.0:8080/);
});

// Wrong way (defaults to localhost only)
const server = app.listen(8080, () => {
    console.log('Server running but only on localhost!');
});

Even with correct binding, you might need to:

sudo ufw allow 8080/tcp  # For Ubuntu's UFW firewall
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT  # For iptables

Verify your server's network configuration:

ifconfig  # Check network interfaces
route -n  # Verify routing table

Use these commands to test from both local and remote:

# Local test
curl http://localhost:8080

# Remote test (from another machine)
telnet your-server-ip 8080
nmap -p 8080 your-server-ip

If you're using cloud services:

  • AWS: Check Security Groups
  • Azure: Verify Network Security Groups
  • Google Cloud: Review Firewall Rules

When standard methods don't work:

# Check for conflicting processes
sudo lsof -i :8080

# Monitor network traffic
sudo tcpdump -i any port 8080

# Test different ports
const server = app.listen(0);  // Random available port

For production deployments, consider:

# Using Nginx as reverse proxy
server {
    listen 80;
    server_name yourdomain.com;
    location / {
        proxy_pass http://localhost:8080;
    }
}

Looking at your netstat output:

tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN          16785/node

This reveals the core issue - your Node.js server is only bound to 127.0.0.1 (localhost) rather than 0.0.0.0 (all network interfaces). This is why external connections can't reach your server.

Modify your Node.js server code to listen on all network interfaces:

const http = require('http');
const port = 8080;

const server = http.createServer((req, res) => {
  res.end('Hello World');
});

// The critical change - use 0.0.0.0 instead of 127.0.0.1
server.listen(port, '0.0.0.0', () => {
  console.log(Server running at http://0.0.0.0:${port}/);
});

After making this change, check your network binding again:

netstat -tulnp | grep :8080
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      12345/node

Now you should see 0.0.0.0 instead of 127.0.0.1, indicating your server is properly accessible.

If you're still having issues, consider these troubleshooting steps:

# Check Ubuntu's built-in firewall
sudo ufw status

# Verify port forwarding (if behind NAT)
sudo iptables -t nat -L

# Test connectivity from another machine
telnet your-server-ip 8080
curl http://your-server-ip:8080

For Express users, here's the proper way to configure your server:

const express = require('express');
const app = express();
const port = 8080;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, '0.0.0.0', () => {
  console.log(Example app listening at http://0.0.0.0:${port});
});

For production environments, you might want to:

  1. Use Nginx as a reverse proxy
  2. Configure proper firewall rules
  3. Set up process management with PM2