Score:1

How to access/view local web server via public IP?

mk flag

Good day everyone. I'm running an Ubuntu desktop 22.04. I'm new to linux world for about a month or so.

I already configure my router for port forwarding.

When I was developing this web app in windows, I got no problem with accessing it via public IP with just configuring my router. I run the web server using nodejs and it's on port 3000.

I could access the web page in my localhost or any computer in my local network just by typing localhost:3000 or 192.168.1.200:3000 but not 102.154.23.2:3000 - an example public IP.

When I run the server. ready - started server on 0.0.0.0:3000, url: http://localhost:3000

This is my ufw configuration.

I dont know if this would help but I'll post this anyway.

What I would want to achieve is to show the develop web app to my client. Is there any way to do this? Thanks in advance!

UPDATE

Steps I did so far to achieve this and after following @user68186 answer:

  • start my web server at port 80 with sudo
  • forward my external and internal IP port to 80 in my router since setting my external IP to port 80 and running my web server at 3000 and setting internal PORT to 3000 will not do the trick.

Follow up question:

Is there some configuration needed to achieve forwarding external IP routes from port 80 to port 3000 with iptables?

Say 102.154.23.2 external IP forwarded to 192.168.1.200:3000 internal IP. Thanks much!

Jhunsen Samonte avatar
mk flag
This also confuse me. I want to access the web page by typing only my public IP. just 119.23.123.2. I also tried adding port 3000 in ufw and still no avail.
Jhunsen Samonte avatar
mk flag
Yes it did work. The reason maybe why I can't view the page is because I set the destination port to 80 not 3000. Now when I type `119.23.123.2:3000` the page shows.
Raffa avatar
jp flag
Not just add port `3000` but rather map destination port `80` to port `3000` ... Web-browsers sent requests to port `80` by default ... so you can forward `119.23.123.2:80` to `your_machines_local_IP:3000`
Raffa avatar
jp flag
That should be done in the router by the way and not on the web-server.
ar flag
@Raffa that is a good point. I will add it to my answer.
ar flag
I have converted my comments to a full answer. Accept it as correct by clicking on the gray check mark ✔️ and turn it green ✅. This will help others.
Score:2
ar flag

Allow port 3000 in firewall

You are not using the default web server ports 80 or 443. As you put it in the question: "I run the web server using nodejs and it's on port 3000." So, you have to open the port 3000.

Open a terminal by pressing Ctrl+Alt+T and enter:

sudo ufw allow proto tcp from any to any port 3000

Forward port 3000 in the router

Make sure port 3000 is forwarded to the Ubuntu server's internal IP address in the home router. This will allow anyone from outside the home LAN access the web server.

The instructions are router specific and beyond the scope of this answer as it has nothing to do with Ubuntu.

enter image description here

Port 80 is the default port for the http protocol. If you want to make it easier for external users to access the web server, you may want to map the router's port 80 to the internal port 3000. This way, external users won't have to add the port number at the end of the external IP address.

enter image description here

The pictures above is just an example. The settings in your router may look different.

Test

Test whether you can access the web server from outside your home network.

Cleanup

Now you may delete the unwanted ufw rules. Use the command:

sudo ufw status numbered

to find the rule number(s) you want to delete, followed by :

    sudo ufw delete N

where N is the number of the rule you want to delete.

Note, the rule numbers may change once you delete a rule. So use the sudo ufw status numbered command again before you delete another rule.

Finally delete the port forwarding rules from your router you do not need.

Hope this helps

Jhunsen Samonte avatar
mk flag
Thank you for your answer! After following your guide, I still can't access the web page even thought I have forwarded port 80 to 3000. Unless I run my web sever at port 80, not 3000, and put the same port 80 at the router's forwarding options for both my external and internal IP, that's when I can access it.
ar flag
@JhunsenSamonte you are welcome. So, it works if you set both internal and external ports to 3000 or both to 80 in the router. But it does not work if you set the external port to 80 and the internal port to 3000 in the router. This is a router problem, not Ubuntu problem. This is beyond the scope of this site.
Jhunsen Samonte avatar
mk flag
Yes, that's seems to be the case. Thank you once again.
Score:0
vn flag

Consider ngrok

ngrok http 80 # Expose a local HTTP service on a given port:

Cloudflared

cloudflared tunnel --url http://localhost:3000

Or some of the others?

Jhunsen Samonte avatar
mk flag
Thank you for your suggestions! I'll look into it.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.