Nick Alexander
Nick Alexander

Reputation: 1653

Cannot access Express web server running on Ubuntu server from outside world

This is probably one of the most common problems on StackOverflow, but I have tried everything that I know to try.

I just purchased my first domain name with intentions to run a web site on Linode. My Linode public IP is 45.79.142.131. Just to test that everything is working, I have set up a simple Express web server that serves a simple Hello, World. Server code:

const express = require('express')
const app = express()

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

app.listen(80, () => console.log('Example app listening on port 80!'))

To allow Express to bind port 80 (note: I will NOT do this for real deployment; this is just a dirty test), I am using the following command:

$ sudo node server-80.js 
Example app listening on port 80!

If I use curl locally on the server, I get a response:

$ curl 45.79.142.131:80
Hello World!

However, if I use a web browser or curl externally, the server does not provide a response.

$ curl 45.79.142.131:80
curl: (7) Failed to connect to 45.79.142.131 port 80: Operation timed out

My first guess was that the firewall was blocking my external request. I am using ufw to manage my firewall. I used `` to allow requests on port 80. I also allowed ssh, https, and a dummy port 3000. I also reloaded the firewall using sudo ufw reload.

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
3000/tcp                   ALLOW       Anywhere                  
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)             
3000/tcp (v6)              ALLOW       Anywhere (v6)  

Running nmap locally on the server provides the following output:

$ nmap localhost -Pn

Starting Nmap 7.60 ( https://nmap.org ) at 2018-08-27 22:12 EDT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00027s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 998 closed ports
PORT   STATE SERVICE
22/tcp open  ssh
80/tcp open  http

Nmap done: 1 IP address (1 host up) scanned in 0.13 seconds

It would appear that port 80 is open. However, if I run nmap externally (i.e. my Mac), I do not see port 80 as being open.

$ nmap 45.79.142.131 -Pn
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-27 22:14 EDT
Nmap scan report for li1241-131.members.linode.com (45.79.142.131)
Host is up (0.096s latency).
Not shown: 999 filtered ports
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 47.51 seconds

I am at a loss. I don't really know what is happening here. What makes things more strange, though, is that if I run my Express web server on port 3000 (and sudo ufw allow 3000/tcp), everything works flawlessly. I am able to get a response from the web server. There is something specific about port 80 that is going wrong. It seems a lot like the firewall is blocking my requests, but I have specifically added an exception for port 80.

If there is any logs or additional information that would help in diagnosing the problem, I am happy to share it.

Upvotes: 1

Views: 2730

Answers (2)

Stan Tatarnykov
Stan Tatarnykov

Reputation: 699

I had a similar issue and what worked for me (on Ubuntu 21.10) was allowing the port the nodejs app is using, through the ubuntu firewall:

sudo ufw allow 8080

To allow all ports (may be less secure), disable the firewall with:

ufw disable

Upvotes: 3

Nick Alexander
Nick Alexander

Reputation: 1653

I thoughtlessly flushed my iptables after setting ufw to default deny incoming traffic. This killed my ssh connection. I completely rebuilt my Ubuntu instance from scratch. When it came time to configure my firewall with ufw, I only allowed ssh and http/tcp. Fortunately, everything appears to be working as expected! I am able to access my dummy web server over port 80.

I don't know what went wrong. The only thing that I can think of is that my iptables somehow got into a badly misconfigured state. Maybe I issued a bad command when I was first learning how to use ufw or something.

Upvotes: 1

Related Questions