大网数据广告活动
服务器运维问答

Exposing or listening to a http port on google cloud instance

· 52CE 我爱测

I have a google cloud instance (Ubuntu 20.04) where I tried to run a nodejs app and expose it to port 80. Here my server.js code: var express = require('express'), app = express(), http = require('http'), httpServer = http.Server…

问题原文

原帖链接 · Syed Arefinul Haque · CC BY-SA 4.0

I have a google cloud instance (Ubuntu 20.04) where I tried to run a nodejs app and expose it to port 80.

Here my server.js code:

var express = require('express'),
  app = express(),
  http = require('http'),
  httpServer = http.Server(app);
var basicAuth = require("express-basic-auth");
app.use(basicAuth({
    users: { 'admin': 'admin' },
    challenge: true
}));
app.use(express.static(__dirname + '/'));
app.get('/', function(req, res) {
  res.sendfile(__dirname + '/index.html');
});
app.listen(80);

Locally it runs perfectly (e.g. localhost:80 in my local machine browser) but in the google cloud instance it produces an error message:

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES: permission denied 0.0.0.0:80
    at Server.setupListenHandle [as _listen2] (net.js:1263:19)
    at listenInCluster (net.js:1328:12)
    at Server.listen (net.js:1415:7)
    at Function.listen (/home/user/development/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/home/user/development/server.js:14:5)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1307:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Then I tried to create a firewall rule so that I can expose a different port from this instance. So I created a firewall rule to allow connections from any ip (0.0.0.0/0) to hit tcp:8000 and added this tagged rule to the instance and restarted it.

Now I changed the server accordingly to expose port 8000:

var express = require('express'),
  app = express(),
  http = require('http'),
  httpServer = http.Server(app);
var basicAuth = require("express-basic-auth");
app.use(basicAuth({
    users: { 'admin': 'admin' },
    challenge: true
}));
app.use(express.static(__dirname + '/'));
app.get('/', function(req, res) {
  res.sendfile(__dirname + '/index.html');
});
app.listen(8000);

This time the server ran without any error message in the google cloud instance but when I hit the public_ip:8000 in my browser the request gets timed out.

Can someone help me configuring the google cloud instance so that it can be run either in port 80 or port 8000?

Doing curl INTERNAL_IP results in

curl: (7) Failed to connect to 10.142.0.5 port 80: Connection refused

Doing curl -p admin:admin localhost:8000 and curl -p admin:admin INTERNAL_IP:8000 returns with printing the index.html file in the terminal.

已采纳解答

原帖链接 · Serhii Rohoza · CC BY-SA 4.0

Have a look at this article:

The TCP/IP port numbers below 1024 are special in that normal users are not allowed to run servers on them. This is a security feature, in that if you connect to a service on one of these ports you can be fairly sure that you have the real thing, and not a fake which some hacker has put up for you.

To run Node.js application on port 80 without NGINX reverse proxy you should at first configure GCP Firewall in the same way as for port 8000 and after that you should choose from the possible solutions below:

  1. Use authbind:
sudo apt update -y
sudo apt install -y authbind
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80
authbind node server.js

replace %user% with your username.

  1. Use PM2 and authbind:
sudo apt update -y
sudo apt install -y authbind
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80

replace %user% with the user that will be running pm2 and add an alias to the user that runs pm2 profile, e.g. ~/.bashrc or ~/.zshrc (note you will need to run source ~/.bashrc or source ~/.zshrc immediately after):

+alias pm2='authbind --deep pm2'

then, run:

pm2 update

and try your app with pm2:

pm2 start app.js
  1. Use iptables to simply redirect traffic:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8000
  1. Use libcap to enable all node programs to bind on any port lower than 1024:
sudo setcap 'cap_net_bind_service=+ep' $(which node)

Do not run your app with root permissions:

$ sudo node app.js

or

$ sudo su -
# node app.js

It's better to use full address path http://PUBLIC_IP:PORT instead of just PUBLIC_IP:PORT in case of any issues.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-09-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。